Laravel5.2 new feature series-definition and use of middleware groups

Source: Internet
Author: User
Laravel5.2 new feature series-definition and use of middleware groups no matter how large your Laravel application is, the volume of routes. php routing files will become larger and larger. To create a new application, you must first split and group the routing files based on the business logic, such as "admin", "auth", and "public. Generally, each part of the group has its corresponding middleware settings. for example, admin will use an auth middleware, and the API Group may have different auth middleware, there will also be a specified middleware that limits the access frequency.

Laravel5.2 introduces the middleware group concept, which is a shortcut for using multiple middleware for routing rules. you only need to define a middleware group key.

Note: Even if you do not want to use the middleware group, you can continue to look at it as this is a major change in Laravel's global middleware stack.

So remember the example of admin mentioned above? Now we can create an admin middleware group for it. next we will detail how to create and use the middleware group.

1. define the middleware Group

You can define the middleware group in app \ Http \ Kernel. php. This class has a new array attribute $ middlewareGroups. the key of this array is the name of the middleware group and the value is the corresponding middleware.

Laravel provides the web and api middleware groups by default:

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',    ],];

As you can see, the Key of $ middlewareGroups can be the middleware class name or the alias of the specified routing middleware, such as throttle or auth. next we will create the admin middleware group:

protected $middlewareGroups = [    'web' => [...],    'api' => [...],    'admin' => [        'web',        'auth',    ]];

We have defined the admin middleware group to use web (another middleware group) and auth (routing middleware alias), which is so simple!

What is different from Laravel 5.1?

You may have noticed that the middleware in the web is the middleware applied to each route in Laravel 5.1 and earlier versions. this is a major ideological improvement: no cookies, sessions, or CSRF functions are available for routes that are not assigned to the web middleware group. For example, this is the case where only the routing of the api middleware group is assigned.

2. use the middleware Group

We have already created a middleware group. how can we use it next?

If you have read routes. php of Laravel 5.2, it will be clear:

Route::get('/', function () {    return view('welcome');});Route::group(['middleware' => ['web']], function () {    //});

As you can see, you can use the middleware group like using the routing middleware: you can set it to either a specified middleware or a middleware array, so we can use the middleware group admin as follows:

Route::group(['middleware' => 'admin'], function () {    Route::get('dashboard', function () {        return view('dashboard');    });});

That's easy!

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.