Laravel Route Cheat Sheet and Usage Guide
This article provides a concise Laravel Route cheat sheet, explaining basic route definitions, resource routes, error handling, parameters, HTTP methods, middleware, named routes, prefixes, namespaces, subdomain routing, and includes practical code examples for each feature.
Reading font
This guide presents a quick reference for Laravel's routing system, covering essential concepts and providing ready-to-use code snippets.
Basic Route Definitions
Route::get('foo', function() {});Route::get('foo', 'ControllerName@function');Route::controller('foo', 'FooController');Resource Routes
Route::resource('posts','PostsController');// Only specific actionsRoute::resource('photo','PhotoController',['only'=>['index','show']]);// Exclude specific actionsRoute::resource('photo','PhotoController',['except'=>['update','destroy']]);// Batch registration of resource routesRoute::resources(['foo'=>'FooController','bar'=>'BarController']);Route::resources(['foo'=>'FooController','bar'=>'BarController'], ['only'=>['index','show']]);Route::resources(['foo'=>'FooController','bar'=>'BarController'], ['except'=>['update','destroy']]);Triggering Errors
App::abort(404);$handler->missing(...) in ErrorServiceProvider::boot();throw new NotFoundHttpException;Route Parameters
Route::get('foo/{bar}', function($bar) {});Route::get('foo/{bar?}', function($bar = 'bar') {});HTTP Request Methods
Route::any('foo', function() {});Route::post('foo', function() {});Route::put('foo', function() {});Route::patch('foo', function() {});Route::delete('foo', function() {});// RESTful resource controllerRoute::resource('foo','FooController');// Register multiple request methods for a routeRoute::match(['get','post'], '/', function() {});Secure Route (TBD)
Route::get('foo', array('https', function() {}));Route Constraints
Route::get('foo/{bar}', function($bar) {})->where('bar','[0-9]+');Route::get('foo/{bar}/{baz}', function($bar,$baz) {})->where(['bar'=>'[0-9]+','baz'=>'[A-Za-z]']);// Global patternRoute::pattern('bar','[0-9]+');HTTP Middleware
// Assign middleware to a routeRoute::get('admin/profile', ['middleware'=>'auth', function() {}]);Route::get('admin/profile', function() {})->middleware('auth');Named Routes
Route::currentRouteName();Route::get('foo/bar', ['as'=>'foobar', function() {}]);Route::get('user/profile', ['as'=>'profile','uses'=>'UserController@showProfile']);Route::get('user/profile','UserController@showProfile')->name('profile');$url = route('profile');$redirect = redirect()->route('profile');Route Prefix
Route::group(['prefix'=>'admin'], function(){
Route::get('users', function(){
return 'Matches The "/admin/users" URL';
});
});Route Namespace
// This route group will use the Foo\Bar namespaceRoute::group(['namespace'=>'Foo\Bar'], function(){});Subdomain Routing
// {sub} will be ignored in the closureRoute::group(['domain'=>'{sub}.example.com'], function(){});For questions about attribution, corrections or content rights, please contactand we will review it promptly.