Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 353
James Jeffery Avatar asked Nov 28 '22 05:11

James Jeffery


1 Answers

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

like image 93
SineCosine Avatar answered Dec 05 '22 23:12

SineCosine