0

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.

Yash
  • 5,225
  • 4
  • 32
  • 65

1 Answers1

0

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.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • So, if I add namespace to the top of my controllers, I would have to reference them in routes.php with full name, right? – Yash May 21 '15 at 14:28
  • If you add the namespace to the top of your controller classes, then they should work. You don’t need to reference them with the full namespace in **routes.php** because they already have the namespace prepended. – Martin Bean May 21 '15 at 14:43
  • That solves the issue, I guess. Thanks a lot :) But now, I am not able to directly access the models in the controller. My models are defined in global namespace, so I know I can access them as /model_name and it works. The problem is, I would have to manually change it for each reference in each controller. Is there a better way of doing that? and should I also define my Models in a separate namespace? Sorry, but I am a beginner at this, so don't have much idea, which would be the better way? – Yash May 21 '15 at 14:59
  • You could put them in a **app/Models/** folder and use the classmap to autoload them, and then namespace them at a future date. Unfortunately, you’re going to have to do some re-factoring somewhere as you’re moving from Laravel 4 that didn’t enforce namespaces, to Laravel 5 which _does_. – Martin Bean May 21 '15 at 15:10