Laravel 4 Nested Controllers and Routing

Can I call a control that is nested in a subfolder 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 do the following:

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

But I get a class error not found. Is there something I am missing as I cannot figure out how to solve this problem from the Laravel 4 documentation.

+4
source share
5 answers

Until you change the namespace of the controller, you must have access to it from the global namespace, even if it is in a subfolder.

So just change:

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

in

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

The file name must also match the class name, so change "AdminController.php" to "LoginController.php" or change the class name from "LoginController" to "AdminController".

And make sure you run composer dump-autoload

+16
source

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

AdminController.php will 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 '); 
+9
source

I had a problem when I saved the admin controller in the application / controllers directory of the / admin controllers directory

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

+4
source

Adding a slash to "app / controller" 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

+2
source

It may be too late, but one possible way is to use namespaces. here is my example: routes.php:

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

and on top of your backend controllers add the following lines:

 namespace backend; use \view as view; 

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

 "app/controllers/backend", "app/controllers/front", 
0
source

Source: https://habr.com/ru/post/1483691/


All Articles