Laravel Routing Basics

Search for a command to run...

No comments yet. Be the first to comment.
A few months ago, my product manager dropped something in the team channel. "We are adding an AI agent to the platform next quarter. It will handle follow-ups automatically." Just like that. One messa

f you’re diving into React, you’ve likely come across the useRef hook. But what exactly is useRef(), and how can it make your life easier? In this post we’ll cover what useRef is, walk through five

A bug that only happens for some users, that you can never reproduce, that isn't in your code. Welcome to the React + browser-translation crash — what causes it, why it's so sneaky, and the battle-tested one-file fix.

Upgrading a core production database is usually the stuff of dev nightmares. It often involves maintenance windows, scheduled downtime, and the looming fear of a botched migration. But what if you cou

We are moving into products that adapt, predict and react with real context. Designing only interfaces is not enough anymore. We are designing experiences that understand the user. Designers and digital agencies are now challenged to move beyond trad...

Engineering at JoBins
167 posts
Welcome to Engineering at JoBins — where we share the stories, insights, and lessons from building a world-class hiring platform. From system design and scalability to developer tools and team culture, our engineers write about the real challenges we solve every day. Whether you're a curious developer or a fellow builder, we hope our experiences inspire and inform your own engineering journey.
Laravel's routing is responsible for passing processing to controllers that correspond to the path and content of an HTTP request.
| File | function |
| api.php | API routing |
| channels.php | Broadcast channel routing |
| console.php | console routing |
| web.php | General 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.
routes/web.php
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
routes/web.php
Route::get('/', fn () => view('welcome'));
If you just want to redirect to view, you can use the view function of Route.
routes/web.php
Route::view('/', 'welcome');
If you want to show some texts only you can do it as well.
routes/web.php
Route::get('health-check', fn () => 'ok');
We should always declare static routes first because any dynamic would be executed first if they're declared before static routes.
routes/web.php
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
routes/web.php
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.
routes/web.php
Route::redirect('here', 'there');
In this example /here redirects to/there`.
Routes can be registered corresponding to HTTP request methods.
routes/web.php
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
routes/web.php
Route::resource('articles', ArticleController::class);
If we use the resource function in the route, it will be the same as writing the following way.
routes/web.php
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('articles.store');
Route::get('articles/{article}', [ArticleController::class, 'show'])->name('articles.show');
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');
For further information please look into the official Laravel documentation https://laravel.com/docs/9.x/routing