好的,我正在使用 Laravel 4.1 ......我正在尝试让我的嵌套 Controller 加载..
当我之前只运行 Composer dump-autoload 的方法时,它在我的本地运行良好,但是在共享托管服务提供商上,我无法运行该命令行...
无论如何,这是我的 composer.json 文件的样子:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.1.*",
"laravelbook/ardent": "2.4.*",
"loic-sharma/profiler": "1.1.*",
"intervention/image": "dev-master",
"mews/purifier": "dev-master"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/controllers/dash",
"app/controllers/dash/product",
"app/models",
"app/models/Product",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-0": {
"App\\Controllers": "app/controllers",
"App\\Controllers\\Dash": "app/controllers/dash",
"App\\Controllers\\Dash\\Product": "app/controllers/dash/product",
"App\\Models": "app/models",
"App\\Models\\Product": "app/models/Product"
}
},
"scripts": {
"post-install-cmd": [
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "dev"
}
然后这里是一个嵌套 Controller (Controllers/dash/MediaController.php)
<?php
namespace App\Controllers\Dash;
class MediaController extends BaseController { }
然后在我的 route ,我有:
Route::group(array('prefix' => 'dash', 'before' => 'auth'), function()
{
Route::controller('media', 'App\Controllers\Dash\MediaController');
});
但它仍然给我这个错误:
Class 'App\Controllers\Dash\MediaController' not found
最佳答案
除非您使用 PSR-4(甚至 PSR-0),否则您必须执行
composer dump-autoload
每次在文件夹中创建新类时。所以 Composer 将这些类添加到
vendor/composer/autoload_classmap.php
文件。查看您的 PSR-0 自动加载结构,我们可以看到您的 Controller 设置为:
"App\\Controllers": "app/controllers",
所以现在composer自动加载系统会尝试找到你的
App\Controllers\Dash\MediaController
在(类似)这个文件夹中的类:/var/www/webapp/app/controllers/App/Controllers/Dash/MediaController.php
这很难,我知道,但 PSR-4 更容易理解,因为
"App\\Controllers\\": "app/controllers",
意味着您的 Controller 文件将在以下位置找到:
/var/www/webapp/app/controllers/Dash/MediaController.php
关于php - 命名空间在 Laravel 4 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999961/