Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Laravel Route Cheat Sheet and Usage Guide

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.

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 actions
Route::resource('photo','PhotoController',['only'=>['index','show']]);
// Exclude specific actions
Route::resource('photo','PhotoController',['except'=>['update','destroy']]);
// Batch registration of resource routes
Route::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 controller
Route::resource('foo','FooController');
// Register multiple request methods for a route
Route::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 pattern
Route::pattern('bar','[0-9]+');

HTTP Middleware

// Assign middleware to a route
Route::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 namespace
Route::group(['namespace'=>'Foo\Bar'], function(){});

Subdomain Routing

// {sub} will be ignored in the closure
Route::group(['domain'=>'{sub}.example.com'], function(){});
About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
Backendroutingweb developmentPHPLaravelroute

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion