企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## **控制器中间件** >[info]支持为控制器定义中间件,你只需要在你的控制器中定义`middleware`属性 app\admin\controller\User.php ~~~ <?php namespace app\admin\controller; use app\BaseController; use think\Request; class User extends BaseController { //每个方法生效 //protected $middleware = ['Auth']; //指定生效的方法 protected $middleware = [ 'app\admin\middleware\Auth' => ['except' => ['hello'] ],//除了hello方法全都生效 'app\admin\middleware\Hello' => ['only' => ['hello'] ],//仅only生效 ]; public function index(Request $request) { dump($request->auth);//auth } public function hello(Request $request) { dump($request->hello);//hello } } ~~~ >[info]对应的中间件 app\admin\middleware\Hello.php app\admin\middleware\Auth.php ~~~ <?php namespace app\admin\middleware; class Hello { public function handle($request, \Closure $next) { $request->hello = 'hello'; return $next($request); } } <?php namespace app\admin\middleware; class Auth { public function handle($request, \Closure $next) { $request->auth = 'Auth'; return $next($request); } } ~~~