Upgrading Laravel 4.2 to 5.0, getting [ReflectionE

2019-08-28 17:15发布

问题:

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.