ThinkPHP5.1 前置操做說明

能夠爲某個或者某些操做指定前置執行的操做方法,設置 beforeActionList屬性能夠指定某個方法爲其餘方法的前置操做,數組鍵名爲須要調用的前置方法名,無值的話爲當前控制器下全部方法的前置方法。php

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

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

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

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

 

示例以下:spa

<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    protected $beforeActionList = [
        'first',  //在執行全部方法前都會執行first方法
        'second' =>  ['except'=>'hello'],  //除hello方法外的方法執行前都要先執行second方法
        'three'  =>  ['only'=>'hello,data'],  //在hello/data方法執行前先執行three方法
    ];
    
    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';
    }
}

 

訪問code

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

最後的輸出結果是blog

first
three
hello

訪問token

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

的輸出結果是:three

first
second
three
data
相關文章
相關標籤/搜索