Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route to controller in subfolder in Laravel 5

This is my routes.php:

Route::get('/', 'Panel\PanelController@index');

This is my folders:

Http/
....Controllers/
................Panel/
....................../PanelController.php

This is my Controller:

namespace App\Http\Controllers;

class PanelController extends Controller {

/* some code here... */

}

This is what I get:

Class App\Http\Controllers\Panel\PanelController does not exist

I tried the "composer dump-autoload" command but still not working...

like image 921
Olivier Zoletti Avatar asked Mar 27 '15 20:03

Olivier Zoletti


3 Answers

The namespace of your class has to match the directory structure. In this case you have to adjust your class and add Panel

namespace App\Http\Controllers\Panel;
//                             ^^^^^

use App\Http\Controllers\Controller;

class PanelController extends Controller {

/* some code here... */

}
like image 195
lukasgeiter Avatar answered Nov 15 '22 02:11

lukasgeiter


Follow three simple steps

  1. append the folder name in the namespace

    namespace App\Http\Controllers\Panel;
    
  2. Add "use App\Http\Controllers\Controller;" to the controller before the class definition

    namespace App\Http\Controllers\Panel;
    use App\Http\Controllers\Controller;
    
  3. Add the appended folder name when invoking the controller in any route

    Route::get('foo','Panel\PanelController@anyaction');
    

There is no need to run "composer dump-autoload"

like image 45
alex t Avatar answered Nov 15 '22 02:11

alex t


You can generate a controller with a subfolder as simple as:

php artisan make:controller Panel\PanelController

It automatically creates proper namespaces and files with directory. And reference it in routes just as mentioned before:

Route::get('/some','Panel\PanelControllder@yourAction');

Happy codding!

like image 1
Mikhail.root Avatar answered Nov 15 '22 02:11

Mikhail.root