Route to controller in subfolder in Laravel 5

旧城冷巷雨未停 提交于 2019-12-10 02:50:12

问题


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...


回答1:


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... */

}



回答2:


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"




回答3:


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!



来源:https://stackoverflow.com/questions/29309615/route-to-controller-in-subfolder-in-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!