在 Laravel 4 中访问包控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14948329/
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
Accessing package controllers in Laravel 4
提问by sam
I have created a package following the "Creating a Package" instructions in the Laravel 4 documentation. After creating the package I have created a "controllers" folder and a routes file. The new file structure is:
我按照 Laravel 4 文档中的“创建包”说明创建了一个包。创建包后,我创建了一个“控制器”文件夹和一个路由文件。新的文件结构是:
/src
/Vendor
/Package
PackageServiceProvider.php
/config
/controllers
/lang
/migrations
/views
routes.php
/tests
/public
I added the routes file to the boot portion of the package service provider:
我将路由文件添加到包服务提供者的引导部分:
public function boot()
{
$this->package('vendor/package');
include __DIR__ . '/../../routes.php';
}
Then added a basic route to the routes file:
然后在路由文件中添加了一个基本路由:
Route::get('/package', function() {
return "Package route test";
});
Visiting my application at the specified route (app.dev/package) returns the expected:
在指定的路径 (app.dev/package) 访问我的应用程序返回预期:
Package route test
Then adding a basic controller call to the route (using the default Laravel controller, "HomeController") works:
然后向路由添加一个基本的控制器调用(使用默认的 Laravel 控制器,“HomeController”)工作:
Route::get('/package', 'HomeController@showWelcome');
I then followed this SO answerfor setting up the controller for the package. I added the src/controllers folder to the composer classmap, then I dumped the autoloader and checked vendor/composer/autoload_classmap.php and found the class is successfully loaded by composer:
然后我按照这个 SO answer来设置包的控制器。我将 src/controllers 文件夹添加到 composer classmap 中,然后我转储了 autoloader 并检查了 vendor/composer/autoload_classmap.php,发现该类已被 composer 成功加载:
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'HomeController' => $baseDir . '/src/controllers/HomeController.php',
);
Now I added the new package controller to the route using the namespace:
现在我使用命名空间将新的包控制器添加到路由中:
Route::get('/package', 'Vendor\Package\Controllers\HomeController@showWelcome');
but this produces an error about not finding the class:
但这会产生关于找不到类的错误:
ReflectionException: Class Vendor\Package\Controllers\HomeController does not exist
I've also tried calling it using the package name:
我也试过使用包名调用它:
Route::get('/package', 'Package::HomeController@showWelcome');
which produces the same error:
产生相同的错误:
ReflectionException: Class Vendor\Package\Controllers\HomeController does not exist
No matter what I try the package cannot access its own controller, which composer confirms is loaded (by viewing vendor/package/autoload_classmap.php).
无论我尝试什么,包都无法访问它自己的控制器,composer 确认已加载(通过查看 vendor/package/autoload_classmap.php)。
Any ideas? I'm not sure if the issue is composer not loading the class, I'm not sure where to start with debugging the problem. I've created another package and repeated the steps here and get the same problem.
有任何想法吗?我不确定问题是否是作曲家没有加载类,我不确定从哪里开始调试问题。我创建了另一个包并重复了这里的步骤并遇到了同样的问题。
I can access the package views from both the package and the app, eg:
我可以从包和应用程序访问包视图,例如:
View::make('package::view');
The problem seems to be between composer loading the controller and Laravel being able to access it.
问题似乎是在 Composer 加载控制器和 Laravel 能够访问它之间。
回答by sam
The mistake was including the controllers path in the route. I had the following:
错误是在路由中包含控制器路径。我有以下几点:
Route::get('/package', 'Vendor\Package\Controllers\HomeController@showWelcome');
The correct usage is:
正确的用法是:
Route::get('/package', 'Vendor\Package\HomeController@showWelcome');
With the namespace included in the controller:
使用控制器中包含的命名空间:
namespace Vendor\Package;
Controller should extend illuminate:
控制器应延长照明:
\Illuminate\Routing\Controllers\Controller
Still can't use the package name (eg: Package::HomeController@showWelcome), but I can using the namespace. yay.
仍然不能使用包名(例如:Package::HomeController@showWelcome),但我可以使用命名空间。好极了。
Problem solved.
问题解决了。
回答by Daniel Cintra
You may try edit your Vendor/Package/composer.json and insert the controllers dir to autoload/classmap:
您可以尝试编辑您的 Vendor/Package/composer.json 并将控制器目录插入到 autoload/classmap:
....
"autoload": {
"classmap": [
"src/migrations",
"src/controllers",
"src/models"
],
"psr-0": {
"Package\Controller": "src/"
}
}
....
After that, open your terminal and from your package root dir do a composer dump-autoload
之后,打开你的终端,从你的包根目录做一个composer dump-autoload
Works for me...
对我有用...
回答by jai
have a look into this git article might be of help https://github.com/jaiwalker/setup-laravel4-package
看看这篇 git 文章可能会有所帮助 https://github.com/jaiwalker/setup-laravel4-package