Definition and use example of Laravel 5.2 new feature middleware Group

Source: Internet
Author: User
Tags auth

Regardless of the size of the Laravel application you created, 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.

Laravel 5.2 introduces the middleware group concept. This is a shortcut for using multiple middleware for routing rules. You only need to define one 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 \ assumerrorsfromsession: 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 (['ddleware '=> ['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 (['ddleware '=> '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.