<?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';
}
}
如注释所言,为TP5的前置操作。
在以下URL中访问:
http://127.0.0.1/demo/public/index.php/index/index/hello
有如下显示:
first
three
hello

本文详细介绍了ThinkPHP5(简称TP5)中的前置操作机制,包括如何定义在控制器方法执行前后进行的操作。通过实例演示了不同前置操作的应用场景,如在所有方法前执行、除了特定方法外的所有方法前执行以及仅在指定方法前执行的情况。


被折叠的 条评论
为什么被折叠?



