Skip links

Laravel Web Routes

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 web 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 Name
GET/clientsindexclient.index
GET/clients/createcreateclient.create
POST/clientsstoreclient.store
GET/clients/{client}showclient.show
GET/clients/{client}/editeditclient.edit
PUT/PATCH/clients/{client}updateclient.update
DELETE/clients/{client}destroyclient.destroy

Redirecting Routes

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

Permanent redirects

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

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');

// Generating Redirects...
return redirect()->route('profile');
Route::get('user/{id}/profile', function ($id) {
    //
})->name('profile');

$url = route('profile', ['id' => 1, 'photos' => 'yes']);

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.

Leave a comment