Laravel for Beginners: Routing Your Application

Arlind Musliu Portrait

Arlind Musliu

January 7, 2024 · 4 min read

Laravel Blogpost Image

What is Routing?

Routing in Laravel is the process of mapping HTTP requests to application logic that handles those requests. When a user clicks a link or types a URL into their browser, Laravel's routing system determines which controller and method should respond. It's like a GPS for your web application, directing traffic to the right destination.

If you're new to this framework, understanding how Laravel handles routing is crucial for creating a seamless and intuitive user experience. In this article, we'll explore the essence of Laravel routing using a blog application as our example.

Defining Routes

Routes in Laravel are defined in the routes/web.php file. This file is where you tell Laravel what to do when it receives a specific type of request (GET, POST, PUT, DELETE, etc.) to a specific URL.

Let's look at some basic route definitions for our blog application:

Displaying All Blog Posts

To show all blog posts, we might have a route like this:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/posts', [PostController::class, 'index']);

Here, Route::get tells Laravel to respond to a GET request. /posts is the URI the user visits, and [PostController::class, 'index'] points to the index method in the PostController which will handle the request. Make sure you also import the PostController with use App\Http\Controllers\PostController;.

Showing a Single Blog Post

To display a single blog post, we would use a route with a parameter:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{post}', [PostController::class, 'show']);

The {post} is a route parameter that Laravel will automatically pass to the show method of the PostController, so it knows which post to display.

Creating a New Blog Post

For creating a new post, we would have two routes: one for showing the form and another for handling the form submission.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/create', [PostController::class, 'create']);
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts/{post}', [PostController::class, 'show']);

The route /posts/create responds to a GET request and shows the user a form. The second route responds to a POST request when the user submits the form, calling the store method to save the new post.

We reordered the routes to ensure they function correctly. It's crucial to maintain this order; otherwise, it may result in failure. For instance, the last route, which depends on the {post} parameter (which can take any value), should be placed below other routes like /posts/create.

Editing an Existing Blog Post

Editing a post also requires two routes:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/create', [PostController::class, 'create']);
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts/{post}', [PostController::class, 'show']);
Route::get('/posts/{post}/edit', [PostController::class, 'edit']);
Route::put('/posts/{post}', [PostController::class, 'update']);

The edit route displays the edit form, and the update route processes the form submission to update the post.

Deleting a Blog Post

To delete a post, you would use a DELETE request:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/create', [PostController::class, 'create']);
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts/{post}', [PostController::class, 'show']);
Route::get('/posts/{post}/edit', [PostController::class, 'edit']);
Route::put('/posts/{post}', [PostController::class, 'update']);
Route::delete('/posts/{post}', [PostController::class, 'destroy']);

This route calls the destroy method in the PostController to delete the specified post.

Resourceful Routing

Laravel provides a shortcut to create all the routes for these standard actions with a single line of code using a resource route:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

// with the resource controller we have everything in one line
Route::resource('posts', PostController::class);

This single line will generate all the necessary CRUD routes for the PostController. This is the command that creates a resource controller with all the methods:

php artisan make:controller PostController --resource

Route Parameters

Laravel routes can have parameters, as we saw with the {post} parameter in some of the examples above. These parameters are passed to the controller methods, allowing you to handle dynamic data, such as identifying which blog post to show or edit.

Named Routes

Laravel routes can also be named, which makes it easier to generate URLs or redirects within the application. Here's how you can name a route:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

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

You can refer to these routes using their name. We already mentioned this when we discussed the Processing Form Input section while learning about the MVC pattern. For example, if we want to redirect the user to the index page:

return redirect()->route('posts.index');

But, if you use a resource controller then you don't need to manually add all the route names because they're automatically created. That's why we strongly suggest that you use resource controllers as much as possible.

Conclusion

Laravel's routing system is powerful and flexible, allowing you to define clear paths for every action in your application. By understanding routes, you can control how your application responds to user requests and guide users through the features and pages of your blog.

Upcoming Articles in the Series

  1. Laravel for Beginners: Middleware

  2. Laravel for Beginners: Authentication and Authorization

  3. Laravel for Beginners: Events and Listeners


Bring Your Ideas to Life 🚀

If you need help with a Laravel project let's get in touch.

Lucky Media is proud to be recognized as a Top Laravel Development Agency

Arlind Musliu Portrait
Arlind Musliu

Cofounder and CFO of Lucky Media

Technologies:

Heading Pattern

Related Posts

Stay up to date

Be updated with all news, products and tips we share!