1

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'));
halfer
  • 19,824
  • 17
  • 99
  • 186
Irfan Ahmed
  • 9,136
  • 8
  • 33
  • 54

3 Answers3

2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kryten
  • 15,230
  • 6
  • 45
  • 68
  • I am sorry if I couldn't explain well, i had no idea that router may be the actual cause, I have implemented your solution, it works like charm. @Kryten +1 for the effort. Many thanks. – Irfan Ahmed Jan 09 '15 at 07:59
  • I have updated my question, I was missing route stuff to show you. – Irfan Ahmed Jan 09 '15 at 08:01
0

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

Job Brown
  • 166
  • 9
  • No, I wasn't using it before, but error is still the same after using it and there is a yellow line comes up saying unused Use statement. – Irfan Ahmed Jan 08 '15 at 14:24
0

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');
    }
}
shazbot
  • 565
  • 6
  • 14
  • Thanks @shazbot, Everything else is working, i am routing correctly and its working as well, but all I need to use controllers with namespaces unfortunately. http://stackoverflow.com/questions/14714848/using-namespaces-in-laravel-4 – Irfan Ahmed Jan 08 '15 at 14:48
  • There's nothing stopping you from using namespaces for controllers - I do it quite successfully in a large project I'm working on. The key is ensuring that all references to the namespaced class are correct - either with a `use` directive in the file where the class is used or, if you're using the class name in a string like in your route example, using the fully-qualified class name. – Kryten Jan 08 '15 at 15:57