Laravel 4 中的包控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14416608/
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
Package Controllers in Laravel 4
提问by Lars Steen
I want to have controllers in my Laravel 4 package, but I can't get the routing to work.
我想在我的 Laravel 4 包中有控制器,但我无法让路由工作。
I've followed the package instructions in the Laravel 4 documentation, and got the routes.php file working with non-controller routes.
我已经按照 Laravel 4 文档中的包说明进行了操作,并将 routes.php 文件与非控制器路由一起使用。
Could someone please give me some instructions on how to get package controllers to work in Laravel 4, it would be very much appreciated.
有人可以给我一些关于如何让包控制器在 Laravel 4 中工作的说明,我将不胜感激。
Thanks in advance.
提前致谢。
Lars
拉斯
// EDIT:
// routes.php
Route::get('admin', 'Package::AdminController@index'); // Does not work
Route::get('admin', function(){ // Works fine
return 'Dashboard';
})
采纳答案by Wing Lian
You'll need to reference the Controller with it's Namespace too
您也需要使用它的命名空间来引用控制器
Route::get('/admin', 'PackageNS\Package\Controllers\AdminController@getIndex');
or even
甚至
Route::controller('PackageNS\Package\Controllers\AdminController', 'admin');
回答by Kevin Perrine
I don't know the specifics of your situation, nor do I know if this is the "proper" way to fix this issue, but since I came across the same problem I figured I'd share how I solved it.
我不知道您的具体情况,也不知道这是否是解决此问题的“正确”方法,但由于我遇到了同样的问题,我想我会分享我是如何解决它的。
I put my package controllers in the controllers subdirectory, so that my directory structure looks like this:
我把我的包控制器放在控制器子目录中,所以我的目录结构看起来像这样:
/src
/Vendor
/Package
PackageServiceProvider.php
/config
/controllers
/lang
/migrations
/views
/tests
/public
Then, I added the controllers folder to my package's composer.json autoload class map.
然后,我将控制器文件夹添加到我的包的 composer.json 自动加载类映射中。
{
"name": "kevin-s-perrine/my-first-packge",
"description": "",
"authors": [
{
"name": "Kevin S. Perrine",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.0.x"
},
"autoload": {
"classmap": [
"src/migrations",
"src/controllers"
],
"psr-0": {
"KevinSPerrine\MyFirstPackage": "src/"
}
},
"minimum-stability": "dev"
}
Finally, I ran composer dump-autoload
in the package's root directory, and then reference the controller by name in the routes file.
最后,我composer dump-autoload
在包的根目录中运行,然后在路由文件中按名称引用控制器。
Route::get('myfirstpackage', 'MyFirstPackageHomeController@getIndex');
回答by Blessing
In your package's service provider, have you included your routes file? I don't believe L4 loads the route file automatically. You can do it anywhere but I suspect this would be the most appropriate place to do it.
在您的包裹的服务提供商中,您是否包含了您的路线文件?我不相信 L4 会自动加载路由文件。你可以在任何地方做,但我怀疑这将是最合适的地方。
public function register()
{
$this->package('vendor/pkgname');
require __DIR__.'/../routes.php';
}
回答by Glenn Plas
Did you do this:
你做了这个了吗:
composer dump-autoload
The autoloader needs to be told about those shiny new classes. I also suggest you check the webserver logs for errors.
自动加载器需要被告知那些闪亮的新类。我还建议您检查网络服务器日志是否有错误。