4

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.

5 Answers 5

16

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

Sign up to request clarification or add additional context in comments.

3 Comments

You answer help me , does same rules apply for models and views?
for model yes.. but for view you need to separate the folders by (.) dot.
You can indeed set a namespace for the controller, you just need to make sure to set every facade, model and the BaseController class into the file
10

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');

Comments

4

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

Comments

2

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

Comments

0

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",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.