Laravel framework is widely used in PHP development, this paper mainly introduces the route setting in Laravel framework in detail, Laravel route configuration is very flexible, how to set up a good route for PHP application is also quite key.

The basic routing

Most of the routes for your application will be defined in the app/routes.php file. The simplest route in Laravel consists of a URI and a closure call.

Basic GET Route

Route::get('/', function()
{
return 'Hello World';
});Copy the code

Basic POST Route

Route::post('foo/bar', function()
{
return 'Hello World';
});Copy the code

Register a route to respond to all HTTP methods

Route::any('foo', function()
{
   return 'Hello World';
});Copy the code

Forces a route to be accessed over HTTPS

Route::get('foo', array('https', function()
{
    return 'Must be over HTTPS';
}));Copy the code

Often you need to generate URLs based on the route. You can do this by using the URL::to method:

$url = URL::to('foo');Copy the code

Routing parameters

Route::get('user/{id}', function($id)
{
return 'User '.$id;
});Copy the code

Optional route parameters

Route::get('user/{name? }', function($name = null) { return $name; });Copy the code

Optional routing parameters with default values

Route::get('user/{name? }', function($name = 'John') { return $name; });Copy the code

Routes with regular expression constraints

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

Routing filter

Routing filters provide a simple way to restrict access to a given route, which is useful when you need to create an authentication zone for your site. The Laravel framework includes several routing filters, such as the Auth filter, auth.basic filter, guest filter, and CSRF filter. They are stored in the app/filters.php file.

Define a routing filter

Route::filter('old', function() { if (Input::get('age') < 200) { return Redirect::to('home'); }});Copy the code

If a response is returned from a route filter, the response is considered to be the response to the request, the route is not executed, and any after filters for the route are cancelled.

Specifies a route filter for a route

Route::get('user', array('before' => 'old', function() { return 'You are over 200 years old! '; }));Copy the code

Specifies multiple routing filters for a route

Route::get('user', array('before' => 'auth|old', function() { return 'You are authenticated and over 200 years old! '; }));Copy the code

Specify route filter parameters

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

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

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

Basic route filter mode

You might want to specify filters for a set of routes based on the URI.

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

In the example above, the admin filter will apply all routes starting with admin/. The asterisk, as a wildcard, will be applied to all character combinations.

You can also constrain schema filters by specifying HTTP methods:

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

The filter class

For advanced filters, you can use a class instead of a closure function. Because the filter class is an IoC container that sits outside of your application, you can use dependency injection in the filter, making it easier to test.

Define a filter class

class FooFilter { public function filter() { // Filter logic... }}Copy the code

Register a class-based filter

Route::filter('foo', 'FooFilter');Copy the code

After routing

Named routes make it easier to specify routes when generating jumps or URLs. You can specify a name for the route like this:

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

You can also specify route names for controller methods:

Route::get('user/profile', array('as' => 'profile', 'uses' => 
'UserController@showProfile'));Copy the code

Now you use the name of the route when generating URLs or jumps:

$url = URL::route('profile');
$redirect = Redirect::route('profile');Copy the code

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

$name = Route::currentRouteName();Copy the code

Routing group

Sometimes you might want to apply filters to a set of routes. Instead of specifying a filter for each route, you can use routing groups:

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

Subdomain routing

Laravel routing can also handle wildcard subdomains and get wildcard arguments from domain names:

Register subdomain routing

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

Routing prefix

A group of routes can be prefixed by using the prefix option in the attribute array:

Example Add a prefix to a routing group

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

Routing model binding

Model binding provides an easy way to inject models into routes. For example, instead of injecting a user ID, you can inject an entire user model instance based on the specified ID. First specify the required model using the Route::model method:

Bind a variable to the model

Route::model('user', 'User');Copy the code

Then, define a route with {user} parameters:

Route::get('profile/{user}', function(User $user)
{
//
});Copy the code

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

Note: If the model instance is not found in the database, a 404 error will be raised.

If you want to specify the behavior you defined that was not found, you can pass a closure to the Model method as a third argument:

Route::model('user', 'User', function()
{
throw new NotFoundException;
});Copy the code

Sometimes you want to use your own method for routing parameters, using the Route::bind method:

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

Raise a 404 error

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

App::abort(404);Copy the code

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

More information about handling 404 exceptions and using custom responses for these errors can be found in the Errors section.

Routing to the Controller

Laravel allows you to route not only to closures, but also to controller classes, and even to create resource controllers.