Laravel Routes: All about Laravel Routes in 2020

In this Laravel article, I will be covering the most useful set of information about Laravel 8 Routes that you will require in almost all projects. If you have some information about how Laravel works, that will be great but, even if you are an absolute beginner at Laravel, this article will be helpful in setting up laravel routes.

All routes are defined in web.php file under routes directory

Laravel Route Methods

The router allows you to register routes that respond to any HTTP verb:

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

GET Request Example 1

Route::get('/clients', function () {
return "Customers List";
});

GET Request Example 2 returning a View

Route::get('/clients', function () {
return view('list_clients')
});

GET Request Example 3, returning a Controller method

Route::get('/clients', [App\Http\Controllers\ClientController::class, 'index']);

POST Request Example

Route::post('/client', [App\Http\Controllers\ClientController::class, 'create']);

PUT Request Example

Route::put('/client/{id}', [App\Http\Controllers\ClientController::class, 'update']);

DELETE Request Example

Route::delete('/client/{id}', [App\Http\Controllers\ClientController::class, 'destroy']);

Creating all HTTP Verb Routes with single line of Code

In the routes configuration file — app/Http/routes.php — add the following to define a Clients resource route.

Route::resource('clients', 'ClientController');

Request TypePathActionRoute NameGET/clientsindexclient.indexGET/clients/createcreateclient.createPOST/clientsstoreclient.storeGET/clients/{client}showclient.showGET/clients/{client}/editeditclient.editPUT/PATCH/clients/{client}updateclient.updateDELETE/clients/{client}destroyclient.destroy

Redirecting Routes

Route::redirect('/here', '/there');

Permanent redirects

Route::redirect('/here', '/there', 301);

301, means permanent redirect, alternatively we can also use permanentRedirect method to return 301 status code.

Route Parameters

Route::get('user/{id}', function ($id) {
return 'User '.$id;
});

Optional parameters

For Optional Parameters add a ‘?’ after parameter. Also make sure to pass a default parameter value.

Route::get('user/{name?}', function ($name = null) {
return $name;
});

Named Routes

Route Names should always be unique. Naming routes helps a lot in complex projects. So as a good practice I will suggest everyone to use Named Routes.

Route::get('user/profile', [UserProfileController::class, 'show'])->name('profile');

Generating url’s to named routes

// Generating URLs...
$url = route('profile');

Fallback Routes

Fallback routes method comes handy when no route is defined for a given url. 404 pages are already handled by laravel, but in case you want to display users some specific data fallback routes are helpful.

Route::fallback(function () {
//
});

That’s all about Routing in Laravel. If you like this article or have any doubts, feel free to comment below! I will reply as soon as possible.

Written by

Full Stack Web Developer, India — Available to take New Projects! Email: anibabbar@gmail.com | Phone: +91–98124–30938 | www.thinkncode.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store