The route to the controller in a subfolder in Laravel 5

These are my .php routes:

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

These are 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 linker-autoload command, but still didn't work ...

+8
php laravel laravel-5 controller
source share
3 answers

The namespace of your class must match the directory structure. In this case, you need to configure your class and add Panel

 namespace App\Http\Controllers\Panel; // ^^^^^ use App\Http\Controllers\Controller; class PanelController extends Controller { /* some code here... */ } 
+18
source share

Follow three easy steps.

  • add folder name to namespace

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

     namespace App\Http\Controllers\Panel; use App\Http\Controllers\Controller; 
  • Add the name of the attached folder when calling the controller in any route

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

No need to run the “autoload linker”

+5
source share

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

 php artisan make:controller Panel\PanelController 

It automatically creates its own namespaces and directory files. And refer to it the same way as mentioned earlier:

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

Happy crack!

0
source share

All Articles