Basic Routing
The vast majority of your application's routes 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
<span style="font-size: 16px;">Route::get('/', function()<br/>{<br/>return 'Hello World';<br/>});<br/></span>
Basic POST route
<span style="font-size: 16px;">Route::post('foo/bar', function()<br/>{<br/>return 'Hello World';<br/>});<br/></span>
Register a route to respond to all HTTP methods
<span style="font-size: 16px;">Route::any('foo', function()<br/>{<br/> return 'Hello World';<br/>});<br/></span>
Force a route to be accessed via HTTPS
<span style="font-size: 16px;">Route::get('foo', array('https', function()<br/>{<br/> return 'Must be over HTTPS';<br/>}));<br/></span>
Often you need to generate URLs based on a route. You can do this by using URL:: to method:
<span style="font-size: 16px;">$url = URL::to('foo');<br/></span>
routing parameters
<span style="font-size: 16px;">Route::get('user/{id}', function($id)<br/>{<br/>return 'User '.$id;<br/>});<br/></span>
optional routing parameters
<span style="font-size: 16px;">Route::get('user/{name?}', function($name = null)<br/>{<br/>return $name;<br/>});<br/></span>
band Optional route parameters with default values
<span style="font-size: 16px;">Route::get('user/{name?}', function($name = 'John')<br/>{<br/>return $name;<br/>});<br/></span>
Routes with regular expression constraints
<span style="font-size: 16px;">Route::get('user/{name}', function($name)<br/>{<br/>//<br/>})<br/>->where('name', '[A-Za-z]+');<br/>Route::get('user/{id}', function($id)<br/>{<br/>//<br/>})<br/>->where('id', '[0-9]+');<br/></span>
Route filters
Route filters provide a simple way to restrict access to specific routes, which is useful when you need to create zones for your site that require authentication. 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 routing filter
Route::filter('old', function() { if (Input::get('age') < 200) { return Redirect::to('home'); } });
If a response is returned from a routing filter, this response is considered a response to this request, and the routing will not be executed. Any information about this routing The after filter will also be de-executed.
Route::get('user', array('before' => 'old', function() { return 'You are over 200 years old!'; }));
Specify a route filter for a route
Route::get('user', array('before' => 'auth|old', function() { return 'You are authenticated and over 200 years old!'; }));
Specify route filter parameters
Route::filter('age', function($route, $request, $value) { // }); Route::get('user', array('before' => 'age:200', function() { return 'Hello World'; }));
When the route filter receives the response $response as the third parameter:
Route::filter('log', function($route, $request, $response, $value) { // });
Patterns for Basic Route Filters
You may want to specify a filter for a set of routes based on a URI.
Route::filter('admin', function() { // }); Route::when('admin/*', 'admin');
In the above example, the admin filter will be applied with all routes starting with admin/. The asterisk acts as a wildcard character and will match all character combinations.
You can also constrain a pattern filter by specifying the HTTP method:
Route::when('admin/*', 'admin', array('post'));
Filter class
For advanced filters, you can use a class instead of a closure function. Because the filter class is an IoC container that lives outside the 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... } }
Register a class-based filter
Route::filter('foo', 'FooFilter');
Named routes
Named routes make it easier to generate jumps or Routes are specified when specifying URLs. You can give a route a name like this:
Route::get('user/profile', array('as' => 'profile', function() { // }));
You can also give a controller method a route name:
Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));
Now you use the route name when generating URLs or redirects. :
$url = URL::route('profile'); $redirect = Redirect::route('profile');
You can use the currentRouteName method to get the name of a route:
$name = Route::currentRouteName();
Route Group
Sometimes you may want to apply a filter to a group of routes. You don't need 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 subdomains and get wildcard parameters from the domain name:
Register subdomain routing
Route::group(array('domain' => '{account}.myapp.com'), function() { Route::get('user/{id}', function($account, $id) { // }); });
Route prefix
A group of routes can add a prefix to the routing group by using the prefix option in the attribute array:
is Add the prefix
Route::group(array('prefix' => 'admin'), function() { Route::get('user', function() { // }); });
to the route group *Route model binding
Model binding provides a simple way to inject models into routes. For example, instead of just injecting a user's ID, you can inject an entire user model instance based on a 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 containing the {user} parameter:
Route::get('profile/{user}', function(User $user) { // });
Since we have bound the {user} parameter to the User model, a User instance will be injected into the route. So, for example, a request for profile/1 will inject a User instance with ID 1.
Note: If this model instance is not found in the database, a 404 error will be raised.
If you want to specify your own behavior that is not found, you can pass a closure as the third parameter to the model method:
Route::model('user', 'User', function() { throw new NotFoundException; });
Sometimes you want to use your own method to handle routing Parameters, you can use the Route::bind method:
Route::bind('user', function($value, $route) { return User::where('name', $value)->first(); });
Trigger 404 error
There are two methods to manually trigger a 404 error in routing. First, you can use the App::abort method:
App::abort(404);
Second, you can throw an instance of SymfonyComponentHttpKernelExceptionNotFoundHttpException.
More information about handling 404 exceptions and using custom responses for these errors can be found in the Errors chapter.
Routing to Controllers
Laravel not only allows you to route to closures, but also to controller classes, and even allows you to create resource controllers.
More information please Access controller documentation.
For more articles related to Laravel framework routing configuration summary and setting tips, please pay attention to the PHP Chinese website!