Use of laravel5 middleware (1)

Source: Internet
Author: User
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.

     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.