James Jeffery
James Jeffery

Reputation: 12599

Laravel 4 Nested Controllers and Routing

Is it possible to call a control that is nested within a sub folder in Laravel 4?

My Controllers are as follows

- Controllers
    - admin
        * AdminController.php
* HomeController.php
* BaseController.php
* ArticleController.php

Below is the code from my AdminController class:

<?php

class LoginController extends BaseController {

    public function showLogin() 
    {
    return View::make('partials.admin.login');
    }
}

In my Routes.php file I am doing the following:

Route::get('/admin', 'admin.LoginController@showLogin');

But I'm getting a Class not found error. Is there anything I'm missing as I can't seem to find out how to solve this problem from the Laravel 4 documentation.

Upvotes: 4

Views: 9451

Answers (5)

Majid Abdolhosseini
Majid Abdolhosseini

Reputation: 2301

may be its too late but one of the possible ways is to use namespaces. here is my sample: routes.php :

Route::group(array('prefix' => 'admin' , 'before' => 'admin' ), function()
{
Route::controller('contacts' , '\backend\ContactController');
...
}

and on top of your backend controllers add add these lines :

namespace backend;
use \view as view;

and also add these lines to your composers.json in classmap directive :

"app/controllers/backend",
 "app/controllers/front",

Upvotes: 0

SineCosine
SineCosine

Reputation: 387

As long as you don't change the namespace of the controller you should be able to access it from the global namespace even if it is in a subfolder.

So just change:

Route::get('/admin', 'admin.LoginController@showLogin');

to:

Route::get('/admin', 'LoginController@showLogin');

The filename also needs to match the class name so change 'AdminController.php' to 'LoginController.php' or change the class name from 'LoginController' to 'AdminController'.

And make sure you do composer dump-autoload

Upvotes: 16

dji
dji

Reputation: 311

Adding a trailing slash to "app/controllers" in composer.json worked for me:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers/",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
},

Then run composer dump-autoload

Upvotes: 2

ahmed hamdy
ahmed hamdy

Reputation: 5169

You just need to add namespace in your AdminController.php file and change name of the class from LoginController to AdminController

AdminController.php will then be:

<?php

    namespace Admin;
    use BaseController;

    class LoginController extends BaseController {

        public function showLogin() 
        {
        return View::make('partials.admin.login');
        }
    }

and change your routes.php to :

Route::get('/admin', 'admin\LoginController@showLogin');

Upvotes: 10

Thomas Welton
Thomas Welton

Reputation: 148

I experienced a problem when I stored my admin controller in a subdirectory of the controllers directory app/controllers/admin

I had to add this directory to the list of autoload classmaps in my composer.json file Then run composer dump-autoload

Upvotes: 4

Related Questions