dongli8862 2017-09-06 07:45
浏览 407
已采纳

Laravel 5中不存在Class App \ Http \ Controllers \ StudentController

I am building a module called Student in Laravel.

I use the routes.php file inside the Student folder to write routes realted to student module..

When I use just Route::get('/list', function () { return view('welcome');}); program working fine without error.

But when I am using Route::get('/list', 'StudentController@list'); there is a error.

Error is,

Class App\Http\Controllers\StudentController does not exist

Folder Structure

enter image description here

Student Controller

namespace App\Student\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class StudentController extends Controller
{
    public function list(){
        echo "Hello"
    }
}

Student Service Provider

namespace App\Student;

use App\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class StudentServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Define the routes for the application.
     *
     * @internal param Router $router
     */

    public function map()
    {
        Route::group([
            'namespace' => $this->namespace,
            'prefix' => 'students',
        ], function ($router) {
            require __DIR__ . '/routes.php';
        });
    }
}
  • 写回答

7条回答 默认 最新

  • dtv55860 2017-09-06 08:08
    关注

    Although laravel is magic at times, it only works if you stick the the default configuration and conventions.

    You can place your controllers anywhere (heck, even load from a database and eval them) but you have to change the configuration accordingly.

    I suspect you have the wrong namespace configured in RouteServiceProvider. By default it is App\Http\Controllers.

    Changing default folder

    If all your controllers will be in the same folder, change it to App\Student\Controllers and forget about it.

    class RouteServiceProvider extends ServiceProvider
    {
        // ...
        protected $namespace = 'App\Student\Controllers';
        // ...
    }
    

    Multiple modules

    If you want to have multiple modules, then change your RotueServiceProvider namespace config to App and in route files use Student\Controllers\StudentController@list

    class RouteServiceProvider extends ServiceProvider
    {
        // ...
        protected $namespace = 'App';
        // ...
    }
    
    Route::get('/list', 'Student\Controllers\StudentController@list');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值