TP5.1修改默認跳轉方法(success,error)的模板頁面和跳轉時間

有使用過TP5的一定對這個界面不陌生,因爲這是框架自帶的跳轉方法(success,error)界面,但是出於業務需求我們往往需要對這個界面進行美化修改,本文就說明一下如何修改TP5.1框架中的默認跳轉頁面、修改自動跳轉時間、修改全局默認跳轉時間
在這裏插入圖片描述

首先我們打開框架目錄中的config/app.php,這個文件存放着框架應用配置,在app.php末尾部分可以找到這樣兩個配置項:

// 默認跳轉頁面對應的模板文件
'dispatch_success_tmpl'  => Env::get('think_path') . 'tpl/dispatch_jump.tpl',
'dispatch_error_tmpl'    => Env::get('think_path') . 'tpl/dispatch_jump.tpl',

可以發現tpl/dispatch_jump.tpl就是success和error跳轉方法的模板文件跳轉位置,如果需要完全自定義自己的跳轉模板可以將路徑改爲自己的模版文件位置,如果想在原有基礎修改的話我們需要打開框架目錄中thinkphp/tpl/dispatch_jump.tpl,打開後便可修改默認的跳轉頁面,我這裏僅對div部分做了簡單的修改,修改後代碼如下:

<div class="text-center" style="margin-top: 250px;">
    <div class="system-message">
        <?php switch ($code) {?>
        <?php case 1:?>
        <img src="/static/images/success.png" alt="success" style="width: 300px;">
        <p class="success"><?php echo(strip_tags($msg));?></p>
        <?php break;?>
        <?php case 0:?>
        <img src="/static/images/error.png" alt="error" style="width: 300px;">
        <p class="error"><?php echo(strip_tags($msg));?></p>
        <?php break;?>
        <?php } ?>
        <p class="detail"></p>
        <h4 class="jump">
            頁面自動 <b><a id="href" href="<?php echo($url);?>">跳轉</a></b> 等待時間: <b id="wait"><?php echo($wait);?></b>
        </h4>
    </div>
</div>

再次使用success/error方法看下效果:
在這裏插入圖片描述

至此就成功的修改了默認跳轉模板,在這個模版中我們可以看到控制自動跳轉時間的參數爲$wait,這個參數的位置我們可以打開thinkphp/library/traits/controller/jump.php,我們可以在這裏找到success和error兩個方法:

protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])

根據方法所需參數,很輕易就可以知道如想要想修改自動跳轉時間可以設置方法第四個參數,例如這樣就可以設置自動跳轉時間爲10秒:

return $this->error('請先登錄管理員賬號','index/index/login','',10);

但是往往不需要我們在每次跳轉時都設置這個參數,我們只需要修改默認的全局自動跳轉時間即可,只需修改thinkphp/library/traits/controller/jump.php中的success/error的默認參數即可,例如想將默認全局自動跳轉時間設置爲5秒的話這樣即可實現:

protected function success($msg = '', $url = null, $data = '', $wait = 5, array $header = [])
protected function error($msg = '', $url = null, $data = '', $wait = 5, array $header = [])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章