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)