:::
5-5 異常處理
- 由於資料過濾檢查若有錯誤,會拋出異常,故我們必須接收之。
- 我們將整個 switch 包起來,已接收執行動作時拋出的異常
try { switch ($op) { case "insert_action": $action_id = insert_action(); header("location:{$_SERVER['PHP_SELF']}?action_id={$action_id}"); exit; default: action_form(); break; } } catch (exception $e) { $xoopsTpl->assign('error', $e->getMessage()); } -
我們將街收到的異常訊息,拋到樣板去,並套用的樣板變數 <{$error}> 中。
-
故我們來製作一個簡易的呈現錯誤樣板 templates/error_alert.tpl
<{if $error}> <div class="alert alert-danger"> <{$error}> </div> <{/if}> -
若不想在xoops_version.php中註冊該樣板,我們可以利用 includeq 語法,引入樣板檔案:
<{includeq file="$xoops_rootpath/modules/模組名稱/templates/樣板.tpl"}> -
編輯現有的樣板 templates/tad_signup_adm_main.tpl 引入之
<div class="container-fluid"> <{includeq file="$xoops_rootpath/modules/tad_signup/templates/error_alert.tpl"}> <{$action_form}> </div> -
若是嫌麻煩,直接轉向也是個不錯的方法:
} catch (exception $e) { redirect_header($_SERVER['PHP_SELF'], 3, $e->getMessage()); } -
或是利用內建的錯誤訊息框也不錯
} catch (exception $e) { xoops_error($e->getMessage(), '錯誤訊息'); }
5-4 建立安全的表單