Upgrading Laravel 4.2 to 5.0, getting [ReflectionException] Class App\Http\Controllers\PagesController does not exist

匆匆过客 提交于 2019-12-25 06:25:28

问题


I was updating my project from laravel 4.2 to laravel 5.0. But, after I am facing this error and have been trying to solve it for the past 4 hours.

I didn't face any error like this on the 4.2 version. I have tried composer dump-autoload with no effect.

As stated in the guide to update, I have shifted all the controllers as it is, and made the namespace property in app/Providers/RouteServiceProvider.php to null. So, I guess all my controllers are in global namespace, so don't need to add the path anywhere.

Here is my composer.json:

"autoload": {
    "classmap": [
        "app/console/commands",
        "app/Http/Controllers",
        "app/models",
        "database/migrations",
        "database/seeds",
        "tests/TestCase.php"
    ],

Pages Controller :

<?php
class PagesController extends BaseController {

  protected $layout = 'layouts.loggedout';

  public function getIndex() {
    $categories = Category::all();

    $messages = Message::groupBy('receiver_id')
                ->select(['receiver_id', DB::raw("COUNT('receiver_id') AS total")])
                ->orderBy('total', 'DESC'.....

And, here is BaseController.

<?php

class BaseController extends Controller {

    //Setup the layout used by the controller.
    protected function setupLayout(){
        if(!is_null($this->layout)) {
            $this->layout = View::make($this->layout);
        }
    }

}

In routes.php, I am calling controller as follows :

Route::get('/', array('as' => 'pages.index', 'uses' => 'PagesController@getIndex'));

Anyone please help. I have been scratching my head over it for the past few hours.


回答1:


Routes are loaded in the app/Providers/RouteServiceProvider.php file. If you look in there, you’ll see this block of code:

$router->group(['namespace' => $this->namespace], function($router)
{
    require app_path('Http/routes.php');
});

This prepends a namespace to any routes, which by default is App\Http\Controllers, hence your error message.

You have two options:

  1. Add the proper namespace to the top of your controllers.
  2. Load routes outside of the group, so a namespace isn’t automatically prepended.

I would go with option #1, because it’s going to save you headaches in the long run.



来源:https://stackoverflow.com/questions/30375950/upgrading-laravel-4-2-to-5-0-getting-reflectionexception-class-app-http-contr

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