Laravel framework route configuration summary and setup tips, laravel framework _ PHP Tutorial

Source: Internet
Author: User
Tags subdomain
Laravel framework route configuration summary and setup skills. The Laravel framework provides a summary of routing configuration and configuration techniques. The vast majority of the routes of your application will be defined in the approutes. php file. The simplest routing configuration and setup skills of Laravel framework in Laravel

Basic route

The vast majority of your application's routes will be defined in the app/routes. php file. The simplest routing in Laravel consists of a URI and a closure call.

Basic GET route

The code is as follows:


Route: get ('/', function ()
{
Return 'Hello World ';
});


Basic POST route

The code is as follows:


Route: post ('foo/bar', function ()
{
Return 'Hello World ';
});


Register a route to respond to all HTTP methods

The code is as follows:


Route: any ('foo', function ()
{
Return 'Hello World ';
});


Force a route to be accessed through HTTPS

The code is as follows:


Route: get ('foo', array ('https', function ()
{
Return 'ust be over https ';
}));


You often need to generate a URL based on the route. you can use the URL: to method:

The code is as follows:

$ Url = URL: to ('Foo ');

Route parameters

The code is as follows:


Route: get ('user/{id} ', function ($ id)
{
Return 'user'. $ id;
});


Optional route parameters

The code is as follows:


Route: get ('user/{name ?} ', Function ($ name = null)
{
Return $ name;
});


Optional route parameters with default values

The code is as follows:


Route: get ('user/{name ?} ', Function ($ name = 'John ')
{
Return $ name;
});


Routes with regular expression constraints

The code is as follows:


Route: get ('user/{name} ', function ($ name)
{
//
})
-> Where ('name', '[A-Za-z] + ');
Route: get ('user/{id} ', function ($ id)
{
//
})
-> Where ('id', '[0-9] + ');

Route filter

The routing filter provides a simple method to restrict access to a specified route, which is useful when you need to create an authentication region for your site. The Laravel framework contains some routing filters, such as auth filter, auth. basic filter, guest filter, and csrf filter. They are stored in the app/filters. php file.

Define a route filter

The code is as follows:


Route: filter ('old', function ()
{
If (Input: get ('age') <200)
{
Return Redirect: to ('Home ');
}
});


If a response is returned from a route filter, the response is considered as the response of the request, the route will not be executed, and any after filter about the route will be canceled.

Specify a route filter for a route

The code is as follows:


Route: get ('user', array ('before' => 'old', function ()
{
Return 'you are over 200 years old! ';
}));


Specify multiple route filters for a route

The code is as follows:


Route: get ('user', array ('before' => 'auth | old', function ()
{
Return 'you are authenticated and over 200 years old! ';
}));


Specify route filter parameters

The code is as follows:


Route: filter ('age', function ($ route, $ request, $ value)
{
//
});
Route: get ('user', array ('before' => 'age: 200 ', function ()
{
Return 'Hello World ';
}));


When the routing filter receives a response as the third parameter $ response:

The code is as follows:


Route: filter ('log', function ($ route, $ request, $ response, $ value)
{
//
});


Basic routing filter mode

You may want to specify a filter for a group of routes based on the URI.

The code is as follows:


Route: filter ('admin', function ()
{
//
});
Route: when ('admin/* ', 'admin ');


In the preceding example, the admin filter adds all routes starting with admin/to the application. An asterisk is used as a wildcard to adapt to the combination of all characters.

You can also specify the HTTP method as the constraint filter:

The code is as follows:


Route: when ('admin/* ', 'admin', array ('post '));

Filters

For advanced filters, you can use a class to replace the closure function. Because the filter class is an IoC container outside the application, you can use dependency injection in the filter to make it easier to test.

Define a filter class

The code is as follows:


Class FooFilter {
Public function filter ()
{
// Filter logic...
}
}


Register a class-based filter

The code is as follows:


Route: filter ('foo', 'foofilter ');

Named route

A named route is easier to specify when a jump or URL is generated. You can specify a name for the route as follows:

The code is as follows:


Route: get ('user/profile ', array ('as' => 'Profile', function ()
{
//
}));


You can also specify the route name for the controller method:

The code is as follows:


Route: get ('user/profile ', array ('as' => 'Profile', 'uses '=>
'Usercontroller @ showProfile '));


Now you can use the route name when generating URLs or redirection:

The code is as follows:


$ Url = URL: route ('Profile ');
$ Redirect = Redirect: route ('Profile ');


You can use the currentRouteName method to obtain the name of a route:

The code is as follows:


$ Name = Route: currentRouteName ();

Route Group

Sometimes you may want to apply the filter to a group of routes. You do not need to specify a filter for each route. you can use the routing group:

The code is as follows:


Route: group (array ('before' => 'auth'), function ()
{
Route: get ('/', function ()
{
// Has Auth Filter
});
Route: get ('user/profile ', function ()
{
// Has Auth Filter
});
});

Subdomain routing

Laravel routing can also process wildcard subdomain names and obtain wildcard parameters from the domain name:

Register a subdomain routing

The code is as follows:


Route: group (array ('domain '=>' {account} .myapp.com '), function ()
{
Route: get ('user/{id} ', function ($ account, $ id)
{
//
});
});

Route prefix

You can add a prefix to a route group by using the prefix option in the attribute array:

Add a prefix to a route Group

The code is as follows:


Route: group (array ('prefix' => 'admin'), function ()
{
Route: get ('user', function ()
{
//
});
});

Route model binding

Model binding provides a simple method to inject a model into a route. For example, you can inject not only one user ID, but also the entire user model instance according to the specified ID. First, use the Route: model method to specify the required model:

Bind a variable to the model

The code is as follows:


Route: model ('user', 'User ');


Then, define a route containing the {user} parameter:

The code is as follows:


Route: get ('Profile/{user} ', function (User $ user)
{
//
});

Because we have already bound the {user} parameter to the User model, a User instance will be injected into the route. Therefore, for example, a profile/1 request will be injected into a User instance with ID 1.

Note: If this model instance is not found in the database, the error 404 is returned.

If you want to specify the unfound behavior that you have defined, you can pass a closure for the model method as the third parameter:

The code is as follows:


Route: model ('user', 'user', function ()
{
Throw new NotFoundException;
});


You can use the Route: bind method to process Route parameters in your own way:

The code is as follows:


Route: bind ('user', function ($ value, $ route)
{
Return User: where ('name', $ value)-> first ();
});


Error 404

There are two ways to manually trigger a 404 error in the route. First, you can use the App: abort method:

The code is as follows:


App: abort (404 );

Second, you can throw an instance of Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException.

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.