How to Create a Prefix in Laravel Route

AuthorSumit Dey Sarkar

Pubish Date21 Mar 2023

categoryLaravel

In this tutorial we will learn how to create a prefix in laravel route.

 

How to create a prefix in laravel route

To create a prefix in Laravel route, you can use the `prefix()` method. Here's an example:

Route::prefix('admin')->group(function () {
    Route::get('dashboard', function () {
        // Matches the "/admin/dashboard" URL
        return view('admin.dashboard');
    });
    
    Route::get('users', function () {
        // Matches the "/admin/users" URL
        return view('admin.users');
    });
});

 

In the above example, we have created a prefix of "admin" using the `prefix()` method. This means that all routes inside the `group` will have "admin" as their prefix. So, the URLs for the two routes will be "/admin/dashboard" and "/admin/users" respectively.

 

Note that you can use any string as your prefix, not just "admin".

Comments 0

Leave a comment