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

drake24's avatar

Registering a Controller outside from the Controllers folder

Hi, I have a question. How can a route find a controller class if it is nested outside from the default Controllers folder. For example here is my controller folder.

    Controllers
    Middleware
    PHICS
          - Controller
                 --MyController.php  <--- I want to call this from my route

Route::get('phicq', 'MyController@showTest'); <-- which is this

0 likes
33 replies
michaeldyrynda's avatar

If your class is properly namespaced, you can do this

Route::get('phicq', 'PHICS\Controller\MyController');
drake24's avatar

Tried it, but got this error. @deringer

Class App\Http\Controllers\PHICS\Controller\MyController does not exist

Route::get('phicq', 'PHICS\Controller\MyController@showTest');

drake24's avatar

It was so easy in Laravel 4.2. I'm getting my bearings with Laravel 5.0. With the folder restructure, it seems there are a lot changes going on.

usman's avatar
usman
Best Answer
Level 27

Inside app/Providers/RouteServiceProvider change the following line:

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

Now inside your routes.php use the full name space to map the controllers e.g:

Route::get('phicq', 'PHICS\Controller\MyController@index');
//or
Route::get('phicq', 'App\Http\Controllers\MyController@index');

Usman.

4 likes
drake24's avatar

Hi @usman tried both still get this error

Class App\Http\Controllers\PHICS\Controller\MyController does not exist.

namespace App\Http\PHICS\Controller; class MyController extends Controller{ public function showTest(){ return "Show Test"; } }

usman's avatar

@drake try by removing your compiled.php by running artisan clear-compiled. Then it should work.

drake24's avatar

Hi @usman tried it but get another error.

ReflectionException in ControllerInspector.php line 28: Class sdad\Auth\AuthController does not exist

londoh's avatar

@drake24 I hit what might be similar problem a couple weeks ago, but then other stuff got priority so I didnt find a solution yet. But I still need to, so interested to know if you figure this out.

here's the (longish) thread I started at the time: https://laracasts.com/discuss/channels/general-discussion/l5-porting-l4-controllers-and-namespaces/

when I looked at it, it seemed to me that l5 has lost the flexibility for controller location that came with l4

I know its not conclusive of anything, but fwiw this L4 quote has gone from L5 docs:

Note: Since we're using Composer to auto-load our PHP classes, controllers may live anywhere on the file system, as long as composer knows how to load them. The controller directory does not enforce any folder structure for your application. Routing to controllers is entirely de-coupled from the file system.

instead there's this...

If you choose to nest or organize your controllers using PHP namespaces deeper into the App\Http\Controllers directory...

I havent looked at framework code, but maybe answer is there.

regards

l.

usman's avatar

@drake24 you need to prefix the Auth\AuthController with App\Http\Controllers\.

E.g:

Route::controllers([
    'auth' => 'App\Http\Controllers\Auth\AuthController',
    'password' => 'App\Http\Controllers\Auth\PasswordController',
]);
drake24's avatar

Hi @usman tried both still get this error

Class App\Http\Controllers\PHICS\Controller\MyController does not exist. Route

Route::get('test1', 'PHICS\Controller\HomeBaseController@showTest');
Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',]);

My Controller which is found at

-Http
   --Controllers
   --Middleware
   --PHICS
        ---Controller
               ---HomeBaseController.php
namespace App\Http\PHICS\Controller;
class HomeBaseController extends Controller{
    public function showTest(){
        return "Show Test";
    }
}

and My RouteserviceProvider

protected $namespace = '';

After which I run and get this error

Class App\Http\Controllers\PHICS\Controller\HomeBaseController does not exist

1 like
usman's avatar

@drake24 ok, try:


Route::get('test1', 'App\Http\PHICS\Controller\HomeBaseController@showTest');
/*Route::controllers([
    'auth' => 'App\Http\Controllers\Auth\AuthController',
    'password' => 'App\Http\Controllers\Auth\PasswordController',
]);*/

and see if HomeBaseController loads, then we will see about auth.

drake24's avatar

@usman

didn't work

ReflectionException in compiled.php line 1026:
Class App\Http\Controllers\App\Http\PHICS\Controller\HomeBaseController does not exist
usman's avatar

@drake24 I am pretty sure the compile.php file is messing with you... did you run the artisan clear-compiled command? anyway remove the compiled.php from the app/storage/framework dir.

drake24's avatar

@usman

Finally!!! I removed it. .. However, if I run the artisan clear-compiled the command doesn't work.

Anyways. What is the purpose of the compile.php? And for the

protected $namespace = '';

Does it mean it searches the entire app/ folder?

And what if it is set as

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

Does it mean it will search their for the controllers?

Thanks!

usman's avatar

@drake24 you can set the $namespace property to App\Http and then you will be able to load your controllers from the default Controllers dir and the PHIC dir by prefixing the controllers with the perticular namespace.

eg: Controllers\SomeController and PHIC\Controllers\SomeController.

Update:// if all of your controllers are inside the App\Http\PHIC namespace then surely you can set it using the $namespace property.

Usman.

usman's avatar

compiled.php is used for better performance you can regenerate it using the artisan optimize command when deploying the application. It compiles all of your frequently used classes inside one big file and saves the class loading times.

drake24's avatar

I see.. Thanks @usman usually when I create apps on Laravel, I usually bundle them inside a folder (the PHIC) and refrain from using the built in ones (app/http/controllers) so that it is easy for me to organize and structure my code. Thanks again!

pmall's avatar

I see.. Thanks @usman usually when I create apps on Laravel, I usually bundle them inside a folder (the PHIC) and refrain from using the built in ones (app/http/controllers) so that it is easy for me to organize and structure my code. Thanks again!

Why ? There is no reason. App folder is already a forlder for your app.

usman's avatar

:) no problem, I am happy to help.

usman's avatar

@pmall is right, btw you can use the artisan app:name command to set the application wide namespace.

drake24's avatar

@pmall Yes, but I prefer to have my own folder structure since I will be dealing with larger applications and namingthem by folder will be easier for me to manage which modules are which.

PHIC
  -Admin
      --Controllers
      --Models
   -Guest
      --Controllers
      --Models

Rather than putting them inside Controllers. So it is more like my own personal preference :)

I have been managing Zend Framework applications from Japanese companies and most of the time they group their apps according to folder structure like what I did. :)

pmall's avatar

You can organize the app folder in everyway you want no need to create subfolders.

mortezapiri's avatar

in route can write global namespace

Route::get('test'',\App\loram\TestController@index');

1 like
baajanssen's avatar

Hi, I'm facing the exact same problem. I have followed all the steps. I want to place certain controllers in the folder Front/Controllers.

Folder structure

PROJECT
  - CMS
      --Controllers
      --Models
   -Front
      --Controllers
      --Views

I have cleared the namespace variable:

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    //protected $namespace = 'App\Http\Controllers';
    protected $namespace = '';

Adjusted the routes

Route::get('/', 'Front\Controllers\FrontPagesController@homePage')->name('front.pages.front.home');
    Route::get('/diensten', 'Front\Controllers\PagesController@servicesPage')->name('front.pages.front.diensten');
    Route::get('/wagenpark', 'Front\Controllers\PagesController@vehiclesPage')->name('front.pages.front.wagenpark');
    Route::get('/contact', 'Front\Controllers\PagesController@contactPage')->name('front.pages.front.contact');
  • I executed artisan clear-compiled command

  • coudnt remove compiled.php from the app/storage/framework, since it is not present in my project.

  • Im running laravel 5.6, are there any changes to how this is done?

Thnx in advance!

Cronix's avatar

@baajanssen And where is PHIC relative to app dir in the directory hierarchy? How are your namespaces set up? Most likely this is a namespace issue.

baajanssen's avatar

Hi @Cronix,

The rest of my project folders are inside the CMS folder, so the controller lies outside the app folder. This is my directory hierarchy, i haven't setup any namespaces yet. I have to mention that i'm pretty new to Laravel ^^


PROJECT
  - CMS
    --app
    --bootstrap
    --config
    --database
    --node_modules
    --public
    --routes
    --storage
    --tests
    --vendor
    --etc...


   -Front
    --Controllers
    --Resources
        --assets
        --views
    --Routes

Next

Please or to participate in this conversation.