标签:
laravel5 的中间件的使用 (1)
1.创建中间件
php artisan make:middleware _Name_Middleware
2.注册中间件
2.1 中间件注册在 Kennel.php 里面
2.2 默认的存放在此处,会自动调用
/** * 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, #可在此处进行注册 如
‘App\Http\Middleware\AfterMiddleware‘,
];
2.3 还有一种触发的方式,就是 从Route进行触发的
2.3.1 首先需要注册他,即
/** * The application‘s route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ ‘auth‘ => \App\Http\Middleware\Authenticate::class, ‘auth.basic‘ => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, ‘guest‘ => \App\Http\Middleware\RedirectIfAuthenticated::class, ‘throttle‘ => \Illuminate\Routing\Middleware\ThrottleRequests::class, #注册一个 中间件 ‘demo.before‘ => ‘App\Http\Middleware\BeforeMiddleware‘, ];
他的使用和默认的有所不同,他想要在 router.php 中 使用 Router:: 的时候触发
<?php Route::group([‘middleware‘ => [‘web‘]], function () { Route::get(‘/‘, function () { return view(‘welcome‘); }); Route::group([‘prefix‘ => ‘demo‘],function(){
Route::get(‘after‘, [‘as‘=>‘home.after.ware‘ , ‘uses‘ => ‘HomeController@after‘]);
}); });
此处的代码是这样子的,当我们 访问的时候,他便会 进行自动使用中间件
3. 主要说说第二种方式的触发,可以看到
Kernel.php 处的 middleware 的 demo.before
‘demo.before‘ => ‘App\Http\Middleware\BeforeMiddleware‘,
与 Router.php 处的 路由 demo --> before 对应
Route::group([‘prefix‘ => ‘demo‘],function(){
Route::get(‘before‘, [‘as‘=>‘home.before.ware‘ , ‘uses‘ => ‘HomeController@before‘]);
});
OK,写的不好之处,往指正
标签:
原文地址:http://www.cnblogs.com/zeopean/p/laravel5.html