Laravel Routing Basics


2 min read

Laravel's routing is responsible for passing processing to controllers that correspond to the path and content of an HTTP request.

Directory structure

api.phpAPI routing
channels.phpBroadcast channel routing
console.phpconsole routing
web.phpGeneral web page routing

We will generally use web.php for web routing. If you want to use api routing then you can do it under api.php.

Returing to view


Route::get('/', function () {
    return view('welcome');

Simply we can pass the first argument as path and the second argument as callback/ closure function. Generally, the second argument is a reference to the controller class because writing business logic inside the callback function might bloat the routing file.

If we don't have any business logic and want to redirect to view simply, we don't need to register the controller. view('welcome') will make reference to resources/views/welcome.blade.php

Callallback function for PHP7.4 or later


Route::get('/', fn () => view('welcome'));

A simple way to write a view

If you just want to redirect to view, you can use the view function of Route.


Route::view('/', 'welcome');

If you want to show some texts only you can do it as well.


Route::get('health-check', fn () => 'ok');

Routing Priority

We should always declare static routes first because any dynamic would be executed first if they're declared before static routes.


Route::get('articles/{id}', [ArticleController::class, 'show']);
Route::get('articles/create', [ArticleController::class, 'create']);

In the above example, when accessing '/articles/create', the route parameter {id} is prioritized instead of create and the show method will be invoked. Correct implementation would be as follows


Route::get('articles/create', [ArticleController::class, 'create']);
Route::get('articles/{id}', [ArticleController::class, 'show']);


If we want to do the simple redirect, the redirect function can be used. The original route will return 302 as a status code.


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

In this example /here redirects to/there`.

HTTP request method

Routes can be registered corresponding to HTTP request methods.


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

Resource Routes


Route::resource('articles', ArticleController::class);

If we use the resource function in the route, it will be the same as writing the following way.


Route::get('articles', [ArticleController::class, 'index'])->name('articles.index');
Route::get('articles/create', [ArticleController::class, 'create'])->name('articles.create');
Route::post('articles', [ArticleController::class, 'store'])->name('');
Route::get('articles/{article}', [ArticleController::class, 'show'])->name('');
Route::get('articles/{article}/edit', [ArticleController::class, 'edit'])->name('articles.edit');
Route::put('articles/{article}', [ArticleController::class, 'update'])->name('articles.update');
Route::delete('articles/{article}', [ArticleController::class, 'destroy'])->name('articles.destroy');

Official Document

For further information please look into the official Laravel documentation