:::

5-5 異常處理

  1. 由於資料過濾檢查若有錯誤,會拋出異常,故我們必須接收之。
  2. 我們將整個 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());
    }
  3. 我們將街收到的異常訊息,拋到樣板去,並套用的樣板變數 <{$error}> 中。

  4. 故我們來製作一個簡易的呈現錯誤樣板 templates/error_alert.tpl

    <{if $error}>
      <div class="alert alert-danger">
        <{$error}>
      </div>
    <{/if}>
  5. 若不想在xoops_version.php中註冊該樣板,我們可以利用 includeq  語法,引入樣板檔案:

    <{includeq file="$xoops_rootpath/modules/模組名稱/templates/樣板.tpl"}>
  6. 編輯現有的樣板 templates/tad_signup_adm_main.tpl 引入之

    <div class="container-fluid">
      <{includeq file="$xoops_rootpath/modules/tad_signup/templates/error_alert.tpl"}>
      <{$action_form}>
    </div>
  7. 若是嫌麻煩,直接轉向也是個不錯的方法:

    } catch (exception $e) {
        redirect_header($_SERVER['PHP_SELF'], 3, $e->getMessage());
    }
  8. 或是利用內建的錯誤訊息框也不錯

    } catch (exception $e) {    
        xoops_error($e->getMessage(), '錯誤訊息');
    }

     


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D873

計數器

今天: 4379437943794379
昨天: 3438343834383438
總計: 7394708739470873947087394708739470873947087394708