Laravel 4 嵌套控制器和路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16847761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 Nested Controllers and Routing
提问by James Jeffery
Is it possible to call a control that is nested within a sub folder in Laravel 4?
是否可以调用嵌套在 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:
下面是我的 AdminController 类的代码:
<?php
class LoginController extends BaseController {
public function showLogin()
{
return View::make('partials.admin.login');
}
}
In my Routes.php file I am doing the following:
在我的 Routes.php 文件中,我正在执行以下操作:
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.
但是我收到了一个 Class not found 错误。有什么我遗漏的,因为我似乎无法从 Laravel 4 文档中找到如何解决这个问题。
回答by SineCosine
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'.
文件名还需要与类名匹配,因此将“AdminController.php”更改为“LoginController.php”或将类名从“LoginController”更改为“AdminController”。
And make sure you do composer dump-autoload
并确保你这样做 composer dump-autoload
回答by ahmed hamdy
You just need to add namespace
in your AdminController.php
file and change name of the class from LoginController
to AdminController
您只需要namespace
在AdminController.php
文件中添加并将类的名称从 更改LoginController
为AdminController
AdminController.php
will then be:
AdminController.php
然后将是:
<?php
namespace Admin;
use BaseController;
class LoginController extends BaseController {
public function showLogin()
{
return View::make('partials.admin.login');
}
}
and change your routes.php
to :
并将您的更改routes.php
为:
Route::get('/admin', 'admin\LoginController@showLogin');
回答by Thomas Welton
I experienced a problem when I stored my admin controller in a subdirectory of the controllers directory app/controllers/admin
当我将管理控制器存储在控制器目录 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
我必须将此目录添加到我的 composer.json 文件中的自动加载类映射列表然后运行 composer dump-autoload
回答by dji
Adding a trailing slash to "app/controllers" in composer.json
worked for me:
在composer.json
对我有用的“应用程序/控制器”中添加尾部斜杠:
"autoload": {
"classmap": [
"app/commands",
"app/controllers/",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
Then run composer dump-autoload
然后运行 composer dump-autoload
回答by mhndev
may be its too late but one of the possible ways is to use namespaces. here is my sample: routes.php :
可能为时已晚,但可能的方法之一是使用名称空间。这是我的示例: 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 :
并将这些行添加到您的 composers.json 中的 classmap 指令中:
"app/controllers/backend",
"app/controllers/front",