- The Basics
- Wildcards
- The 404 Events
- Filters
- Global Filters
- Route Groups
- Named Routes
- HTTPS Routes
- Bundle Routes
- CLI Route Testing
Laravel uses the latest features of PHP 5.3 to make routing simple and expressive. It's a joy to build everything from simple JSON APIs to complex web applications. Routes are typically defined in application/routes.php.
Route::get('/', function()
{
return "Hello World!";
});
Route::any('/', function()
{
return "Hello World!";
});
Route::post('user', function()
{
//
});
Route::put('user/(:num)', function($id)
{
//
});
Route::delete('user/(:num)', function($id)
{
//
});
Note: Routes are evaluated in the order that they are registered, so register any "catch-all" routes at the bottom of your routes.php file.
Route::get('user/(:num)', function($id)
{
//
});
Route::get('post/(:any)', function($title)
{
//
});
Route::get('page/(:any?)', function($page = 'index')
{
//
});
If a request enters your application but does not match any existing route, the 404 event will be raised. You can find the default event handler in your application/routes.php file.
Event::listen('404', function()
{
return Response::error('404');
});
You are free to change this to fit the needs of your application!
Futher Reading:
Route filters may be run before or after a route is executed. If a "before" filter returns a value, that value is considered the response to the request and the route is not executed, making it a breeze to implement authentication filters, etc.
Route::filter('filter', function()
{
return Redirect::to('home');
});
Route::get('blocked', array('before' => 'filter', function()
{
return View::make('blocked');
}));
Route::get('download', array('after' => 'log', function()
{
//
}));
Route::get('create', array('before' => 'auth|csrf', function()
{
//
}));
Route::get('panel', array('before' => 'role:admin', function()
{
//
}));
Laravel has two "global" filters that run before and after every request to your application. You can find them both in the application/routes.php file. These filters make great places to start common bundles or add global assets.
Note: The after filter receives the Response object for the current request.
Route groups allow you to attach a set of attributes to a group of routes, allowing you to keep your code neat and tidy.
Route::group(array('before' => 'auth'), function()
{
Route::get('panel', function()
{
//
});
Route::get('dashboard', function()
{
//
});
});
Constantly generating URLs or redirects using a route's URI leads to brittle code. Assining the route a name gives you a convenient way to refer to the route throughout your application.
Route::get('/', array('as' => 'home', function()
{
return "Hello World";
});
$url = URL::to_route('home');
return Redirect::to_route('home');
Once you have named a route, you may easily check if the route handling the current request has a given name.
if (Request::route()->is('home'))
{
// The "home" route is handling the request!
}
When defining routes, you may use the "https" attribute to indicate that the HTTPS protocol should be used when generating a URL or Redirect to that route.
Route::get('login', array('https' => true, function()
{
return View::make('login');
}));
Route::secure('GET', 'login', function()
{
return View::make('login');
});
You can easily setup bundles to handle requests to your application. Let's go back to the application/bundles.php file and add something:
return array(
'admin' => array('handles' => 'admin'),
);
Notice the new handles option in our array? This tells Laravel to load the Admin bundle on any requests where the URI begins with "admin".
Now you're ready to register some routes for your bundle, so create a routes.php file within the root directory of your bundle and add the following:
Route::get('(:bundle)', function()
{
return 'Welcome to the Admin bundle!';
});
Let's explore this example. Notice the (:bundle) place-holder? That will be replaced with the value of the handles clause that you used to register your bundle. This keeps your code D.R.Y. and allows those who use your bundle to change it's root URI without breaking your routes! Nice, right?
Of course, you can use the (:bundle) place-holder for all of your routes, not just your root route.
Route::get('(:bundle)/panel', function()
{
return "I handle requests to admin/panel!";
});
You may test your routes using Laravel's wonderful "Artisan" CLI. Simple specify the request method and URI you want to use. The route response will be var_dump'd back to the CLI.
php artisan route get api/user/1