Example of Laravel HTTP route naming and routing Group

Source: Internet
Author: User
Tags auth closure php file subdomain subdomain name

1. Route name -- name the route

1.1 Basic use

We use the as keyword to name the route:

Route: get ('/hello/laravelemy Emy', ['as' => 'army', function (){
Return 'Hello LaravelAcademy! ';
}]);

Routing naming makes it easier to use the route function to generate a URL pointing to the route or generate a redirect link to the route:

Route: get ('/testNamedRoute', function (){
Return route ('adsory ');
});
We output http://laravel.app: 8000/hello/laravelacademy when accessing http://laravel.app: 8000/testNamedRoute in a browser, then we modify the code in the closure above:

Route: get ('/testNamedRoute', function (){
Return redirect ()-> route ('emy Emy ');
});

When you access http://laravel.app: 8000/testNamedRoute in your browser again, the http://laravel.app: 8000/hello/laravelacademy is redirected.

We can even use a route name with parameters:

Route: get ('/hello/laravelacademy/{id}', ['as' => 'adsory', function ($ id ){
Return 'Hello laravelemy Emy '. $ id .'! ';
}]);
The corresponding test route is defined as follows:

Route: get ('/testNamedRoute', function (){
Return redirect ()-> route ('army', ['id' => 1]);
});

In this way, when we access http://laravel.app: 8000/testNamedRoute in a browser, it will jump to the http://laravel.app: 8000/hello/laravelacademy/1

1.2 Route naming method for route groups

Let's look at a more complex example. How do I define a route name when using a route Group? The official website provides the following examples:

Route: group (['as' => 'admin: '], function (){
Route: get ('dashboard', ['as' => 'dashboard', function (){
//
}]);
});
Use the as keyword in the group method of the Route facade to specify the public prefix of all routes in the Route group, and then use the as keyword in each Route to name the Route.

In this way, the route URL can be generated as follows:

Route: get ('/testNamedRoute', function (){
Return route ('admin: dashboard ');
});

2. Route Group

A routing group aggregates a group of routes with the same attributes (middleware, namespace, subdomain name, and Route prefix) using the group method of Route facade.

2.1 middleware

First, run the following Artisan command in the application root directory to generate a test middleware TestMiddleware:

Php artisan make: middleware TestMiddleware
In this case, a TestMiddleware. Php file is generated under the/app/Http/Middleware Directory. Open the file and edit the handle method of the TestMiddleware class as follows:

Public function handle ($ request, Closure $ next)
{
If ($ request-> input ('age') <18)
Return redirect ()-> route ('refuse ');
Return $ next ($ request );
}
In middleware, we define this business logic to prevent access by minors under 18 years old.

Then open the/app/Http/Kernal. Php file and add the $ routeMiddleware attribute from TestMiddleware to Kernel:

Protected $ routeMiddleware = [
'Auth' => \ App \ Http \ Middleware \ Authenticate: class,
'Auth. basic '=> \ Illuminate \ auth \ Middleware \ AuthenticateWithBasicAuth: class,
'Guest '=> \ App \ Http \ Middleware \ RedirectIfAuthenticated: class,
'Test' => \ App \ Http \ Middleware \ TestMiddleware: class,
];
Next we define a route in routes. php as follows:

Route: group (['middleware Ware '=> 'test'], function (){
Route: get ('/write/laravelacademy', function (){
// Use Test middleware
});
Route: get ('/update/laravelacademy', function (){
// Use Test middleware
});
});

Route: get ('/age/refuse', ['as' => 'refuse', function (){
Return "Minors are not allowed to enter! ";
}]);
So when we access http://laravel.app in a browser: 8000/write/laravelacademy? Age = 15 or http://laravel.app: 8000/update/laravelacademy? Age = 15 will jump to the http://laravel.app: 8000/age/refuse, and display:

Minors are not allowed to enter the website!

2.2 namespace

By default, the controller defined in routes. php is located in the App \ Http \ Controllers namespace. To specify a namespace, you only need to specify the part after App \ Http \ Controllers:

Route: group (['namespace' => 'laravelemy Emy '], function (){
// The controller is in the namespace of "App \ Http \ Controllers \ LaravelAcademy"

Route: group (['namespace' => 'docs'], function ()
    {
// The controller is in the "App \ Http \ Controllers \ LaravelAcademy \ DOCS" namespace
});
});

2.3 subdomain name

Subdomain names can be set using the domain keyword:

Route: group (['domain '=>' {service}. laravel. App'], function (){
Route: get ('/write/laravelacademy', function ($ service ){
Return "Write FROM {$ service}. laravel. app ";
});
Route: get ('/update/laravelacademy', function ($ service ){
Return "Update FROM {$ service}. laravel. app ";
});
});
In this way we access the http://write.laravel.app in the browser: 8000/write/laravelacademy, then the output

Write FROM write. laravel. app
When accessing http://update.laravel.app: 8000/write/laravelacademy, the output is:

Write FROM update. laravel. app
Note: To make the sub-domain name resolution take effect, you need to bind an IP address in hosts.

2.4 route prefix

If all the routes in the routing group contain a uniform prefix, you can specify the prefix by setting the prefix attribute in the group method:

Route: group (['prefix' => 'laravelemy Emy '], function (){
Route: get ('write', function (){
Return "Write LaravelAcademy ";
});
Route: get ('update', function (){
Return "Update LaravelAcademy ";
});
});

In this way, we can access the corresponding operation through http://laravel.app: 8000/laravelacademy/write or http://laravel.app: 8000/laravelacademy/update.

We can even specify parameters in the route prefix:

Route: group (['prefix' => 'laravelemy emy/{version} '], function (){
Route: get ('write', function ($ version ){
Return "Write LaravelAcademy {$ version }";
});
Route: get ('update', function ($ version ){
Return "Update LaravelAcademy {$ version }";
});
});
In this way we access the http://laravel.app in the browser: 8000/laravelacademy/5.1/write, the corresponding will output:

Write laravelacademia 5.1

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.