前置操作

可以為某個或者某些操作指定前置執(zhí)行的操作方法,設(shè)置 beforeActionList屬性可以指定某個方法為其他方法的前置操作,數(shù)組鍵名為需要調(diào)用的前置方法名,無值的話為當前控制器下所有方法的前置方法。

['except' => '方法名,方法名']

表示這些方法不使用前置方法,

['only' => '方法名,方法名']

表示只有這些方法使用前置方法。

示例如下:

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    protected $beforeActionList = [
        'first',
        'second' =>  ['except'=>'hello'],
        'three'  =>  ['only'=>'hello,data'],
    ];
    
    protected function first()
    {
        echo 'first<br/>';
    }
    
    protected function second()
    {
        echo 'second<br/>';
    }
    
    protected function three()
    {
        echo 'three<br/>';
    }

    public function hello()
    {
        return 'hello';
    }
    
    public function data()
    {
        return 'data';
    }
}

訪問

http://localhost/index.php/index/Index/hello

最后的輸出結(jié)果是

first
three
hello

訪問

http://localhost/index.php/index/Index/data

的輸出結(jié)果是:

first
second
three
data
文檔最后更新時間:2018-04-26 08:32:40

文檔
目錄

深色
模式

切換
寬度