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

jmrecodes's avatar

Using different namespace for controllers and how App\Http\Controllers works.

Hello. I am still very new to Laravel, and I wonder if you could use a different namespace for controllers.

I just learned what namespace and use are. Apparently controllers in Laravel uses 'App\Http\Controllers' as its namespace. I am not really sure how Laravel is able to check which classes / Controllers are using the same namespace 'App\Http\Controllers', since from what I learned about namespaces you need to reference which file you are trying to access a namespace from when using "use 'namespace'" by using 'require Filename.php'.

I hope someone is kind enough to enlighten my innocence. Thanks in advance.

0 likes
17 replies
Sergiu17's avatar

Would it help you somehow if you will change the namespace?

Cronix's avatar

With namespaces, you don't actually use require filename.php. It replaces that functionality. The namespace is the path to the file.

App\Http\Controllers is the directory that the controllers live in. That's how it knows what file to load.

jmrecodes's avatar

@Sergiu17 For now, I still don't have any idea of how to take advantage of a custom namespace since I just learned what namespace is, but I would love to know if ever it is possible or not since having freedom in any framework or platform you code from really encourages me to learn more from it. Would still love to learn it though even if it is not possible.

Also, I am very curious as to how Laravel was able to check which classes / Controllers are using the same namespace without using the php function 'require' to specify that class in order to be able to make that class inherit the Controller class and become a custom Controller.

jmrecodes's avatar

@Cronix But from what I learned from quora https://www.quora.com/What-is-namespaces-and-use-in-Laravel and php.net http://php.net/manual/en/language.namespaces.php you need to include the file you are referencing in 'use' method in order to really use that namespace. However, in Laravel the name used in namespace for Controllers is 'App\Http\Controllers' which is the location of the controllers, but in fact you can supposedly use any name for your namespace.

But then when you use a different namespace for your controllers, it will result into an error that won't be solved (I think) unless you use the correct namespace 'App\Http\Controllers' which is coincidentally the location of the controllers as you said in your comment, so I am just really curious if is it possible to change this or not.

jlrdw's avatar

Way too much thought put into this, of course you can have a controller in a different folder. Wouldn't it be easier for you to just Tinker around for a while play around and do some experiments to see how it all works.

jmrecodes's avatar

@jlrdw I am sorry that I wasn't very specific about my question, but what I meant by different is as different as 'MyCustomNamespace' and alike names for namespaces for Controllers. Because I already tried changing it, searching on the web if someone did change theirs, but to no avail I found no solution at all. So I was wondering if someone used a different namespace for their controllers here.

Cronix's avatar

Well laravel uses composers autoloader, which uses the namespaces. I think there are only 2 include statements in laravel, and they're both in /public/index.php.

One loads composers autoloader and the other bootstraps the framework.

I'm not sure if you can change the dir of the controllers. In your routes, you do things like

Route::get('some-endpoint', 'SomeController@someMethod');

Using reflection, Laravel looks in /app/Http/Controllers to see if SomeController exists. You can have other directories/namespaces within the /app/Http/Controllers, but I don't think you can change the default location where it looks for controllers to begin with. It might be possible with a setting somewhere, I haven't ever needed/wanted to change it. I just use subdirs in the controllers dir if I need more separation.

App\Http\Controllers\Admin
App\Http\Controllers\Frontend
App\Http\Controllers\Api

etc.

Then if I had a route that needed an admin controller (actually I use a namespaced route group for this, but for illustration...)

Route::get('some-endpoint', 'Admin\SomeController@someMethod');
1 like
jmrecodes's avatar

@Cronix Thank you very much for your insights. It greatly helped me understand how Laravel work, but I hope someone can confirm if this is ever possible and also about how Laravel really looks for classes / Controllers that uses 'App/Http/Controllers' namepace.

I think you already answered the second question though that it is by 'reflection' that Laravel was able to find the controllers in that directory, but I hope it can be broken into simple English terms for beginners.

Cronix's avatar
Cronix
Best Answer
Level 67

Looks like I was wrong. Look at app/Providers/RouteServiceProvider and change

protected $namespace = 'App\Http\Controllers';

to wherever you want your controllers to live. I'm not sure why you really want to do this though. It makes sense that it's in the dir that it's in... Http.

1 like
Cronix's avatar

However, in Laravel the name used in namespace for Controllers is 'App\Http\Controllers' which is the location of the controllers, but in fact you can supposedly use any name for your namespace.

That's not quite true. The namespace MUST be the dir structure. Like you can't have \app\Http\Controllers\CoolController.php, and within CoolController have namespace Whatever\I\Want\Here. It has to be namespace App\Http\Controllers because that's the dir it was in.

If you had a file in /app/Models/SomeModelName.php, the namespace in the SomeModelName.php file must be 'App\Models', and when you go to import that model from somewhere in your code in order to use it, you'd need to use App\Models\SomeModelName. This is just how namespaces work and organize code. It doesn't have to do with laravel, per se.

1 like
Cronix's avatar

So namespace can also refer to directory.

Not can... must. The namespace is the directory structure of the file that it's in.

jmrecodes's avatar

I don't think that it is a 'must' since it still works whatever name you give to your namespaces, it's just that in Laravel it doesn't and I don't know why, but at least now I know how (app/Providers/RouteServiceProvider, from your reply). Take a look at the php.net and quora links I gave above.

And I also tried it for myself:

Controller.php (Default Controller, located @ app/Http/Controllers)

<?php
namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

require 'NewController.php';
use App\NewController as target;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    
    public function index() {
        $controller1 = new target();
        
        echo $controller1->shownumber();
    }
}

NewController.php (not really a custom Controller just a simple class with a namespace, still located @ app/Http/Controllers)

<?php
namespace App;

class NewController
{
    public function shownumber() {
        return 1;
    }
}

Update: To clarify, I wasn't able to create a custom controller here since I already tried doing it many times but to no avail it just won't work with a different namespace, and I think it still won't even if you already answered as to where to change it since you'll still have to follow that naming convention of a namespace (its directory). What I was trying to prove to myself in this blocks of code is whether my understanding of Namespaces and Use really is correct or not.

Cronix's avatar

it's just that in Laravel it doesn't and I don't know why

It's not "laravel". It's "composer" and its autoloader, which uses the namespaces.

jmrecodes's avatar

It's just that I was learning about Controllers in Laravel then I saw how all the controllers are required to have the same namespaces in order to be able extend the class to become a controller. From what information I got, namespaces are just names used to organize your files' classes into one place. So that got me wonder whether I can change the namespaces used in the Controllers to whatever name I want it to be not just depending on its directory.

But I guess you really can't. Last thing that still a little bit confuses my mind is as to how the Custom Controllers know how to extend the class to the Controller class just by having the same namespace 'App\Http\Controllers'. You answered this already though, by saying that it's just how namespaces work. And I realized that maybe it really is, it's just that most explanations of namespaces focuses on how you can name your php file a namespace and use those classes in that namespace using the 'use' keyword.

It just doesn't highlight about how it 'can' also function by naming your namespace as its directory. I still haven't found a clear explanation about this, so I am not really sure if it that's just really how namespace work or it's just that way how Laravel works.

Cronix's avatar

There are several pieces of the puzzle. PHP Namespaces, Laravel, and Composer. I think you need to check a little more on the composer end, specifically how the autoloader works, which is what Laravel's using. It might fill in the missing gaps.

secondman's avatar

Yeah I would recommend reading up on autoloading in PHP so you'll better understand how classes and files are loaded.

PSR

Please or to participate in this conversation.