gpt4 book ai didi

php - 命名空间在 Laravel 4 中不起作用?

转载 作者:行者123 更新时间:2023-12-04 17:00:28 25 4
gpt4 key购买 nike

好的,我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com