What is middleware? Listen to the extermination division too interesting to say Laravel middleware

Source: Internet
Author: User

What is middleware? Listen to the extermination division too interesting to say laravel middleware (figure)

This article mainly describes what is middleware? Listen to the wonderful teacher is too interesting to say laravel middleware (figure), has a certain reference value, now share to everyone, there is a need for friends can refer to

What is middleware? Middleware as the name implies is the middle of the pieces, the middle layer, filtering some of the requests, such as some simple token verification; The following article explains in detail the methods and techniques of laravel defining middleware and registering middleware.

I. Defining middleware

The laravel of the callout can be defined directly inside the command.

PHP Artisan Make:middleware Checktoken

This creates a middleware class called Checktoken, which is in the/app/http/middleware directory:

The code will look like this after the point is opened:

Then you can go inside and write something. For example:

Public function handle ($request, Closure $next) {    if ($request->input (' token ')! = ' Minxintoken ') {        return Redirect ()->to (' http://www.baidu.com ');    }    Return $next ($request);}

It is obvious that if the ' token ' field is not equal to Minxintoken, it will automatically jump to http://www.baidu.com.

Of course, that's all. Just create a middleware called Checktoken, and it is not enabled; Start now to enable;

Two. Register middleware

The so-called enable is to register the middleware in the specified route;

The registration is divided into two kinds, the global middleware and the specified routing middleware,

1. Global Middleware:

As long as the corresponding middleware added to the app/http/kernel.php array attribute $middleware in OK: (the original text directly written Checktoken::class is not the report App\http\checktoken does not exist, Need to add checktoken.php namespace to get to checktoken correctly)

The positive solution is as follows:

The browser directly input sample.test/about will jump directly to www.baidu.com, but this play will not:

OK global registration ends here. Generally speaking, the global setting is not recommended, because it is really annoying to take tokens every time;

2. Then take a look at the middleware for the specified route,

This kind of middleware, first need to assign to the middleware a key in the app/http/kernel.php, add in the file $routemiddleware inside, the original inside already have many default key, append one on the following line:

After the key is added, the path is opened by the file.

Route::get ('/', ' Staticpagescontroller@home ')->middleware (' token ');

So the effect of access to the browser is directly sample.test/will redirect to Baidu, and the loss of Sample.test/?token=minxintoken can go to the homepage

Of course, there is a drawback is to a route, to use will knock, not normal; So there's another way to do this is to have an array called $middlewaregroups in the kernel.php file: We add the checktoken we just created in the Web,

Then all the/route/web.php inside will be token=minxintoken to access, such as what Sample.test/sample.test/about sample.test/help and so on;

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.