Laravel5.0 Image: http://stackphp.com/
As shown in, the central green area is the core area of the entire application.
Therefore, middleware is a series of methods for processing requests and responses, not part of your application logic.
Laravel uses middleware to process encryption and decryption in requests, as well as Cookies and Sessions by default. You can also customize the middleware you need.
Write middleware
Artisan make: middleware MyMiddleware
Run the preceding command to generate the middleware file:
Complete the content of the handle method (use the request port as an example ):
server->get('REMOTE_PORT') / 2) % 2 > 0) { throw new \Exception("WE DON'T LIKE ODD REMOTE PORTS"); } return $next($request); }}Use middlewareLaravel 5 has two methods to add middleware, both written in App \ Providers \ AppServiceProvider.
By default, there are two attributes: $ middleware and $ stack. the middleware in $ stack processes each response, while the middleware in $ middleware processes on demand.
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', ];protected $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\Reader', 'Illuminate\Session\Middleware\Writer', ];
Middleware on demand:
protected $middleware = [ 'auth' => 'App\Http\Middleware\AuthMiddleware', 'auth.basic' => 'App\Http\Middleware\BasicAuthMiddleware', 'csrf' => 'App\Http\Middleware\CsrfMiddleware', 'guest' => 'App\Http\Middleware\GuestMiddleware', 'absurd' => 'App\Http\Middleware\MyMiddleware', ];
Middleware route comment
Write directly to the controller class:
/** * @Resource("foobar/photos") * @Middleware("auth") * @Middleware("absurd", except={"update"}) * @Middleware("csrf", only={"index"}) */class FoobarPhotosController{} Writing method:
/** * @Middleware("auth.basic") */public function index() {} $ This-> middleware ()
You can use $ this-> middleware () in the constructor or method to load the corresponding middleware.
...use Illuminate\Routing\Controller;class AwesomeController extends Controller { public function __construct() { $this->middleware('csrf'); $this->middleware('auth', ['only' => 'update']) }} Set middleware in routes. php
// Routes.php// Single route$router->get("/awesome/sauce", "AwesomeController@sauce", ['middleware' => 'auth']);// Route group$router->group(['middleware' => 'auth'], function() { // lots of routes that require auth middleware});Process before and after responseMiddleware processing before and after the application responds:
Previously handled:
...class BeforeMiddleware implements Middleware { public function handle($request, Closure $next) { // Do Stuff return $next($request); }} After processing:
...class AfterMiddleware implements Middleware { public function handle($request, Closure $next) { $response = $next($request); // Do stuff return $response; }}