Laravel 5.0-middleware (middleware)

Source: Internet
Author: User

Image: http://stackphp.com/

As shown, the green area of the center is the core area of the entire application.

So, middleware is a series of ways to handle requests and responses rather than part of your program logic.

Middleware is used by default in Laravel to handle encryption and decryption in requests, as well as Cookies and Sessions. You can also customize the middleware you need.

Write middleware

artisan make:middleware MyMiddleware

Execute the above command to generate the middleware file:

<?php namespace App\http\middleware;use closure;use illuminate\contracts\routing\middleware;class MyMiddleware    Implements middleware {/** * Handle an incoming request.  * * @param \illuminate\http\request $request * @param \closure $next * @return Mixed * * Public function Handle ($request, Closure $next) {//}}

Refine the content of the handle method (use the request port to do an example):

<?php namespace app\http\middleware;use closure;use illuminate\contracts\routing\ middleware;class mymiddleware implements middleware {    /**      * Handle an incoming request.     *      *  @param   \Illuminate\Http\Request   $request       *  @param   \Closure   $next      * @ Return mixed     */    public function handle ($ request, closure  $next)     {        //  Test for an even vs. odd remote port         if  (($request->server->get (' Remote_port ')  / 2)  % 2 >  0)         {            throw new  \exception ("We don ' T like odd remote ports");         }        return  $next ($request);     }}
Using middleware

Laravel 5 has two methods of joining middleware, all written in App\providers\appserviceprovider.

By default there are two properties $middleware and the $stack middleware inside the $stack is processed every time the response occurs, while the middleware in the $middleware is handled on demand.

In a bunch of cases the default middleware is as follows:

protected  $stack  = [         ' app\http\middleware\ Maintenancemiddleware ',         ' Illuminate\cookie\middleware\guard ',          ' Illuminate\cookie\middleware\queue ',          ' Illuminate\session\middleware\reader ',          ' Illuminate\session\middleware\writer ',     ];p rotected  $middleware  =  [         ' auth '  =  ' app\http\middleware\authmiddleware ',          ' auth.basic '  =>  ' app\http\middleware\ Basicauthmiddleware ',         ' csrf '  =>  ' app\http\ Middleware\csrfmiddleware ',         ' guest '  =>  ' app\http\ Middleware\guestmiddleware ',     ]; 

The middleware processes each request:

protected $stack = [' App\http\middleware\mymiddleware ' app\http\middleware\maintenancemiddleware ', ' Illuminate\cookie\middleware\guard ', ' illuminate\cookie\middleware\queue ', ' illuminate\session\middleware\r Eader ', ' illuminate\session\middleware\writer ',];

Middleware on-demand processing:

protected $middleware = [' auth ' = ' app\http\middleware\authmiddleware ', ' auth.basic ' = ' app\http\mi ' Ddleware\basicauthmiddleware ', ' csrf ' = ' app\http\middleware\csrfmiddleware ', ' guest ' = ' App\http\mi Ddleware\guestmiddleware ', ' absurd ' = ' app\http\middleware\mymiddleware ',];

Middleware routing annotations

Write directly to the Controller class:

/** * @Resource ("Foobar/photos") * @Middleware ("auth") * @Middleware ("absurd", except={"Update"}) * @Middleware ("CSRF", only={"Index"}) */class foobarphotoscontroller{}

Write to Method:

/** * @Middleware ("auth.basic") */public function Index () {}

$this->middleware ()

You can use $this->middleware () within a constructor or method to load the appropriate middleware.

Illuminate\routing\controller;class Awesomecontroller extends Controller {public Function __construct () {        $this->middleware (' csrf '); $this->middleware (' auth ', [' only ' = ' Update ']}}

routes.php Setting up middleware

routes.php//single Route$router->get ("/awesome/sauce", "[email protected]", [' middleware ' = ' auth ']);// Route Group$router->group ([' middleware ' = ' auth '], function () {//lots of routes that require auth middleware}) ;
Response before and after processing

Before and after the application responds to middleware processing:

Before processing:

... class Beforemiddleware implements middleware {public function handle ($request, Closure $next) {//Do Stu    FF return $next ($request); }}

After processing:

... class Aftermiddleware implements middleware {public function handle ($request, Closure $next) {$response        = $next ($request);    Do stuff return $response; }}


Laravel 5.0-middleware (middleware)

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.