Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

princeoo7's avatar

What does a namespace actually do in a laravel route ?

i was reading laravel documentation and found that we have a section of namespace in that for route. what does it stands for in practical ?

link: https://laravel.com/docs/5.8/routing#route-groups

Namespaces

Another common use-case for route groups is assigning the same PHP namespace to a group of controllers 


    using the namespace method:

    Route::namespace('Admin')->group(function () {
        // Controllers Within The "App\Http\Controllers\Admin" Namespace
    });

    Remember, by default, the RouteServiceProvider includes your route files within a namespace group, 
allowing you to register controller routes without specifying the full App\Http\Controllers namespace 
prefix. So, you only need to specify the portion of the namespace that comes after the base
App\Http\Controllers namespace.

what i did after reading the section was this ...

Route::prefix('admin/blog')->namespace('Article')->middleware('auth')->group(function () {
    Route::get('create', function () {
        return 'now creating blog';
        });
});

so what will Article namespace do for my application ?

0 likes
6 replies
Jaytee's avatar

Have a read of this. This explains what namespaces are in PHP. Namespaces aren't just used in PHP, they're used in a lot of other languages.

https://daylerees.com/php-namespaces-explained/

The namespace on the route, is to define where the controller is for that route in Laravel. You don't have to use ->namespace(), you can do Route::get('something', 'MyNamespace\SomeController@method');

princeoo7's avatar

@JAYTEE - Thank you for your reply. I know what namespace are and the thing i actually wanted to know is the role of name space in the route code.

well we normally do it like this ( I have been doing it like this since i started laravel development ) :

Route::get('admin/blog/create', 'ArticleController@create' )->name('blog.create');  

so as per your definition, laravel implements it by default. then why explicitly define the name spacing here ? any particular scenario for this ?

OriOn's avatar
OriOn
Best Answer
Level 14

out of the box laravel uses the default namespace for the controller (the one in which artisan places it when you use the command make:controller App\Http\Controllers

if your application happens to have a lot of controller, Let's say you want to build Todo lists for your users we will call Task the model

we create a Todo folder in App\Http\Controllers

<?php

namespace App\Http\Controllers\Todo;

use App\Http\Controllers\Controller;

class TaskController extends Controller
{
    public function index() 
    {
        // ... 
    }
}

in that case in your routes/web.php to refer to that controller's index method you would have two options

    // without namespace
    Route::get('/todo', 'Todo/TaskController@index');

    // with namespace
    Route::namespace('Todo')->get('/todo', 'TaskController@index');

I usually use that to keep the routes a simple a possible putting namespaces, name, middlewares on a parent group.

6 likes
princeoo7's avatar

@ORION - Thank you very much. Now this is a very clear answer to my question. it's exactly what i was looking for :)

Cronix's avatar

@princeoo7 Please mark the post solved. Hover over the post with the answer and click the "Best Answer" button that appears in the upper-right hand corner of the post.

Please or to participate in this conversation.