Irfan Ahmed
Irfan Ahmed

Reputation: 9384

Laravel namespaces class not found

I am newbie to Laravel namespaces.

I am trying to do something like this:

namespace App\Controllers; // when I remove this line, everything works fine... I need to include this

class HomeController extends BaseController {

    protected $layout = "layouts.main";

    public function __construct() {
        // some stuff here
    }

    /**
     * Home page.
     * @return View
     */
    public function getHome() {
        // Show the page
        $this->layout->content = View::make('home');
    }
}

But I am having this weird error,

Class HomeController does not exist 

Here is some of my composer.json stuff,

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/libraries",            
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
    ]
}, 

I have also executed,

composer dump-autoload

While I am routing something like this,

# Default
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@getHome'));

Upvotes: 1

Views: 1252

Answers (3)

Kryten
Kryten

Reputation: 15780

Here's one common case where this error can occur:

Imagine you're defining a route using that class:

Route::get('/home', 'HomeController@getIndex');

The Laravel core is going to pass that string ('HomeController@getIndex') to some method(s) deep in the bowels of the routing class, parse it, instantiate HomeController, and call getIndex. Does that code include a use App\Controllers directive? Not likely, since this is something you've created. Basically, wherever that HomeController class is used (and I have no idea where it is), the PHP interpreter is not going to know where that class comes from.

The solution is to use the fully-qualified class name. That means including the full namespace in the string, like so:

Route::get('/home', '\App\Controller\HomeController@getIndex');

Now when Laravel tries to instantiate the class, it has everything it needs to find it.

I don't know for sure if this is the problem - you need to show us the code where the error occurs - but this is one possibility.

Upvotes: 2

shazbot
shazbot

Reputation: 585

You don't use namespaces for Controllers in app/controllers. HomeController extends BaseController which extends Controller in the framework.

You might want to use namespaces if you are including custom libraries into the framework. /app/libraries for example.

As long as your routes.php has some url to get to a home controller method, it should work.

Route::get('/home', 'HomeController@index');

HomeController.php:

class HomeController extends BaseController {

    private $myVar;

    public function __construct() {
        // some stuff here
    }

    public function index(){
        return View::make('home');
    }
}

Upvotes: 0

Job Brown
Job Brown

Reputation: 166

Are you typing use App\Controllers\HomeController in the file where you're trying to use it? This essentially includes it.

Upvotes: 0

Related Questions