Laravel framework route configuration summary and setup skills

Source: Internet
Author: User
Tags subdomain

Laravel framework route configuration summary and setup skills

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
Copy codeThe Code is as follows:
Route: get ('/', function ()
{
Return 'Hello world ';
});

Basic POST route
Copy codeThe Code is as follows:
Route: post ('foo/bar', function ()
{
Return 'Hello world ';
});

Register a route to respond to all HTTP methods
Copy codeThe Code is as follows:
Route: any ('foo', function ()
{
Return 'Hello world ';
});

Force a route to be accessed through HTTPS
Copy codeThe 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:
Copy codeThe Code is as follows: $ url = URL: to ('foo ');

Route Parameters
Copy codeThe Code is as follows:
Route: get ('user/{id} ', function ($ id)
{
Return 'user'. $ id;
});

Optional route Parameters
Copy codeThe Code is as follows:
Route: get ('user/{name ?} ', Function ($ name = null)
{
Return $ name;
});

Optional route parameters with default values
Copy codeThe Code is as follows:
Route: get ('user/{name ?} ', Function ($ name = 'john ')
{
Return $ name;
});

Routes with regular expression Constraints
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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

Copy codeThe 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
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe 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:

Copy codeThe 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
Copy codeThe Code is as follows:
Class FooFilter {
Public function filter ()
{
// Filter logic...
}
}

Register a class-based Filter
Copy codeThe 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:
Copy codeThe Code is as follows:
Route: get ('user/profile ', array ('as' => 'profile', function ()
{
//
}));

You can also specify the route name for the Controller method:
Copy codeThe 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:

Copy codeThe 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:

Copy codeThe 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:
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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
Copy codeThe Code is as follows:
Route: model ('user', 'user ');

Then, define a route containing the {user} parameter:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:

Copy codeThe 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.