Laravel Framework Routing Configuration Summary Setup Skills

  • 2021-07-18 07:26:02
  • OfStack

Basic routing

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

Basic GET routing


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

Basic POST routing

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

Register 1 route in response to all HTTP methods

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

Force 1 route to be accessed through HTTPS

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

Often you need to generate URLs based on the route, you can use the URL:: to method:
$url = URL::to('foo');

Routing parameters


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

Optional routing parameters

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

Optional routing parameters with default values

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

Routing 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]+');

Routing filter

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

Define 1 routing filter


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 a response to the request, the route is not executed, and any after filters for the route are canceled.

Specify 1 route filter for 1 route


Route::post('foo/bar', function()
{
return 'Hello World';
});
0
Specify multiple route filters for 1 route


Route::post('foo/bar', function()
{
return 'Hello World';
});
1
Specify routing filter parameters

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

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

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

Mode of basic routing filter

You may want to specify filters for 1 group of routes based on URI.


Route::post('foo/bar', function()
{
return 'Hello World';
});
4
In the above example, the admin filter will apply all routes starting with admin/. As a wildcard, the asterisk will be adapted to the combination of all characters.

You can also constrain the schema filter by specifying the HTTP method:


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

Filter class

For advanced filters, you can use 1 class instead of closure functions. Because the filter class is an IoC container located outside of the application, you can use dependency injection in the filter, making it easier to test.

Define 1 filter class


Route::post('foo/bar', function()
{
return 'Hello World';
});
6
Register 1 class-based filter

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

Named route

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


Route::post('foo/bar', function()
{
return 'Hello World';
});
8
You can also specify the route name for the method of the controller:

Route::post('foo/bar', function()
{
return 'Hello World';
});
9
Now you use the name of the route when generating URLs or jump:


Route::any('foo', function()
{
   return 'Hello World';
});
0
You can use the currentRouteName method to get the name of 1 route:


$name = Route::currentRouteName();

Routing group

Sometimes you may want to apply filters to Group 1 routes. You don't have to specify filters for each route, you can use route groups:


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 handle wildcard subdomain names and get wildcard parameters from domain names:

Register subdomain routing


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

Routing prefix

1 Group of routes can be prefixed by using the prefix option in the property array:

Add a prefix to a routing group


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

Routing model binding

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

Bind a variable to the model


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

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

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

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

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

If you want to specify a behavior you define that is not found, you can pass a closure as the third parameter to the model method:


Route::any('foo', function()
{
   return 'Hello World';
});
7
Sometimes you want to handle routing parameters in your own way, you can use the Route:: bind method:

Route::any('foo', function()
{
   return 'Hello World';
});
8
Throw a 404 error

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


App::abort(404);

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 Controller

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

Please visit the controller documentation for more information.


Related articles: