Laravel5.1框架注册中间件的三种场景详解
程序员文章站
2023-11-22 12:09:52
本文实例讲述了laravel5.1框架注册中间件的三种场景。分享给大家供大家参考,具体如下:
在laravel中注册中间件主要有三种场景,一种给控制器中的方法进行注册...
本文实例讲述了laravel5.1框架注册中间件的三种场景。分享给大家供大家参考,具体如下:
在laravel中注册中间件主要有三种场景,一种给控制器中的方法进行注册,一种是给整个控制器进行注册,最后一种是给全局注册中间件。
- 1、在控制器中的方法中注册中间件
这种需求是最为常见,这个例子是给indexcontroller中的index方法添加中间件。
route::get('/', ['middleware'=>['app\http\middleware\emailmiddleware'],'uses'=>'indexcontroller@index']);
- 2、在整个控制器中注册中间件
这种需求有的时候也会出现,如果给整个控制器中注册中间件,那么这个控制器中的所有方法都注册了该中间件。跟我一起来找到答案!
控制器基类(controller.php)
首先来看一下控制器基类,可以看到controller类继承了basecontroller类,而basecontroller的路径是illuminate\routing\controller,然后我们来看看laravel的源码。
<?php namespace app\http\controllers; use illuminate\foundation\bus\dispatchesjobs; use illuminate\routing\controller as basecontroller; use illuminate\foundation\validation\validatesrequests; use illuminate\foundation\auth\access\authorizesrequests; abstract class controller extends basecontroller { use authorizesrequests, dispatchesjobs, validatesrequests; }
basecontroller类
从源码中我们可以看到有一个属性为middleware,这个middleware属性就是我们要找的,所以只要在需要注册的控制器下使用这个属性即可完成注册。
protected $middleware = [];
<?php namespace illuminate\routing; use badmethodcallexception; use symfony\component\httpkernel\exception\notfoundhttpexception; abstract class controller { /** * the middleware registered on the controller. * * @var array */ protected $middleware = []; /** * register middleware on the controller. * * @param array|string|\closure $middleware * @param array $options * @return \illuminate\routing\controllermiddlewareoptions */ public function middleware($middleware, array $options = []) { foreach ((array) $middleware as $m) { $this->middleware[] = [ 'middleware' => $m, 'options' => &$options, ]; } return new controllermiddlewareoptions($options); } /** * get the middleware assigned to the controller. * * @return array */ public function getmiddleware() { return $this->middleware; } /** * execute an action on the controller. * * @param string $method * @param array $parameters * @return \symfony\component\httpfoundation\response */ public function callaction($method, $parameters) { return call_user_func_array([$this, $method], $parameters); } /** * handle calls to missing methods on the controller. * * @param array $parameters * @return mixed * * @throws \symfony\component\httpkernel\exception\notfoundhttpexception */ public function missingmethod($parameters = []) { throw new notfoundhttpexception('controller method not found.'); } /** * handle calls to missing methods on the controller. * * @param string $method * @param array $parameters * @return mixed * * @throws \badmethodcallexception */ public function __call($method, $parameters) { throw new badmethodcallexception("method [{$method}] does not exist."); }
给整个控制器注册中间件
我们选用系统的authenticate中间件来举例,这个中间件是用于检测用户是否登录。
注意:中间件的名称为键,值可以是一个空数组
protected $middleware = ['\app\http\middleware\authenticate'=>[]];
- 3、全局注册中间件
打开app/http/kernel.php,这是一个内核文件,可以看到一个属性$middleware,我们只需要将我们自定义的中间件的路径添加到这个$middleware 这个属性中即可。
此外还有一个$routemiddleware属性,使用这个属性可以根据路由来注册中间件。
我们的路由有:goods/info,goods/detail两个路由器
我们可以将$routemiddleware属性添加一行
'goods.*' => \app\http\middleware\goodsmiddleware::class,
<?php namespace app\http; use illuminate\foundation\http\kernel as httpkernel; class kernel extends httpkernel { /** * the application's global http middleware stack. * * @var array */ protected $middleware = [ \illuminate\foundation\http\middleware\checkformaintenancemode::class, \app\http\middleware\encryptcookies::class, \illuminate\cookie\middleware\addqueuedcookiestoresponse::class, \illuminate\session\middleware\startsession::class, \illuminate\view\middleware\shareerrorsfromsession::class, \app\http\middleware\verifycsrftoken::class, ]; /** * the application's route middleware. * * @var array */ protected $routemiddleware = [ 'auth' => \app\http\middleware\authenticate::class, 'auth.basic' => \illuminate\auth\middleware\authenticatewithbasicauth::class, 'guest' => \app\http\middleware\redirectifauthenticated::class, ]; }