Use of laravel5 middleware (1) use of laravel5 middleware (1)
1. create middleware
Php artisan make: middleware _ Name_Middleware
2. Register middleware
2.1 middleware registered in Kennel. php
2.2 By default, it is stored here and will be called automatically
/*** 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, # You can register here, as shown in figure
'App\Http\Middleware\AfterMiddleware',
];
2.3 another method of triggering is to trigger from Route.
2.3.1 First, you need to register him, that is
/*** The application's route middleware. ** These middleware may be assigned to groups or used inpidually. ** @ 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, # register a Middleware 'demo. before '=> 'app \ Http \ Middleware \ BeforeMiddleware',];
His usage is different from the default one. he wants to trigger this function when using router: In Router. php.
['web']], function () { Route::get('/', function () { return view('welcome'); }); Route::group(['prefix' => 'demo'],function(){
Route::get('after', ['as'=>'home.after.ware' , 'uses' => 'HomeController@after']);
}); });
The code here is like this. when we access it, it will automatically use the middleware.
3. let's talk about the second method of triggering. we can see that
Demo. before of middleware at Kernel. php
'Demo. before' => 'app \ Http \ Middleware \ BeforeMiddleware ',
Corresponds to the routing demo --> before at Router. php
Route::group(['prefix' => 'demo'],function(){
Route::get('before', ['as'=>'home.before.ware' , 'uses' => 'HomeController@before']);
});
OK. What's wrong with writing? correct it.