Laravel Middleware Middleware Notes

Source: Internet
Author: User

Laravel Middleware makes it easy to filter requests for access to our web pages. This includes user authorization, cors to specify headers for outgoing requests, and so on.

To define a new middleware you can use the command:

make: Middleware agemiddleware

这条命令在app/Http/Middleware中创建AgeMiddleware类。

Let's say we filter all requests that are younger than 200:

<?phpnamespace App\http\middleware; UseClosure;classagemiddleware{/** * Run the request filter. * * @param \illuminate\http\request $request * @param \closure $next * @return Mixed*/     Public functionHandle$request, Closure$next)    {        if($request->input (' age ') <= 200) {            returnRedirect (' home ')); }        return $next($request); }}

You can pass the request down by calling the $next callback function.

Middleware filtering is not limited to the request arrival, when the request processing is complete, can also be filtered through the middleware processing:

<?phpnamespace App\http\middleware; UseClosure;classbeforemiddleware{ Public functionHandle$request, Closure$next)    {        //Perform Action        return $next($request); }}<?phpnamespace App\http\middleware; UseClosure;classaftermiddleware{ Public functionHandle$request, Closure$next)    {        $response=$next($request); //Perform Action        return $response; }}

If you need your middleware to be able to filter all Http requests, simply list them in the app/http/kernel.php $middleware.

If you need to assign middleware to a specific route, you first need to declare a simple key in the $routemiddleware in app/http/kernel.php:

protected $routeMiddleware = [    ' auth ' = \app\http\middleware\authenticate::class,    ' Auth.basic ' = \illuminate\auth\middleware\authenticatewithbasicauth::class,    ' guest ' and \app\ Http\middleware\redirectifauthenticated::class,    ' throttle ' = \illuminate\routing\middleware\ Throttlerequests::class,];

Once declared, it can be used in the route, and the route may also specify multiple middleware:

function  () {    //}]); Route function  () {    //}]);

You can also specify by method:

function () {    //})->middleware ([' First ', ' second ']);

We can also declare multiple middleware group in app/http/kernel.php as a middleware, such as our well-known web and API middleware:

protected $middlewareGroups= [    ' Web ' =[\app\http\middleware\encryptcookies::class,\illuminate\cookie\middleware\addqueuedcookiestoresponse::class,\illuminate\session\middleware\startsession::class,\illuminate\view\middleware\shareerrorsfromsession::class,\app\http\middleware\verifycsrftoken::class,    ], ' API ' = [        ' throttle:60,1 ', ' Auth:api ',    ],];

Middleware also accepts the arguments, just add the new parameters after the last variable $next. The parameters need to be used in route: separate middleware and arguments:

<?phpnamespace App\http\middleware; UseClosure;classrolemiddleware{/** * Run the request filter.  * * @param \illuminate\http\request $request * @param \closure $next * @param string $role * @return Mixed*/     Public functionHandle$request, Closure$next,$role)    {        if(!$request->user ()->hasrole ($role)) {            //Redirect ...        }        return $next($request); }}route::p ut (' Post/{id} ', [' middleware ' = ' role:editor ',function($id) {    //}]);

Laravel Middleware Middleware Notes

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.