Backend Development 5 min read

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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Laravel Route Cheat Sheet and Usage Guide

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(){});
backendroutingweb developmentphpLaravelRoute
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.