首页

laravel全局中间件

因为业务需要想搞一个全局的拦截器,因为现有的框架在laravel基础上搞了个架子,业务代码和路由全在vendor目录不受git管理,这时候就想到全局中间件了。 操作如下: 1、在app/Http/Middleware目录下加一个中间件,当然也可以直接用命令行生成,暂且叫TestMiddleware.php吧。 2、TestMiddleware必须继承Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode。 3、需要在app/Http/Middleware/Kernel.php中的 $middleware 数组里面调用一下\App\Http\Middleware\TestMiddleware::class。 TestMiddleware.php代码如下: <code> &lt;?php namespace App\Http\Middleware; Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as BaseMiddleware; use Closure; class TestMiddleware extends BaseMiddleware{ public function handle($request, Closure $next) { //要执行的操作 return $next($request); } } </code> app/Http/Middleware/Kernel.php代码如下: <code> &lt;?php namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { /** * The application's global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */ protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \App\Http\Middleware\TrustProxies::class, <span style="color:green">\App\Http\Middleware\TestMiddleware::class,</span> ]; .................其他代码....................... </code> 测试了一下是可行的。然鹅后面发现其实这个架子有提供配置中间件的地方,只要自己写个路由中间价再把自定义的中间件名称加进去就可以了[裂开]!
更多>>
关于前面laravel查询bug的后续解决 记一次docker日志引起的运维事故 mysql临时关闭binlog php字符串转文件流 杂谈js和php的变量赋值区别