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

时间:2014-02-24 21:59:09

标签: php laravel namespaces composer-php

好的我正在使用Laravel 4.1 ...我正试图让我的嵌套控制器加载.. 当我在执行我之前运行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"
}

然后这是一个嵌套控制器(Controllers / dash / MediaController.php)

<?php

namespace App\Controllers\Dash;

class MediaController extends BaseController { }

然后在我的路线中,我有:

Route::group(array('prefix' => 'dash', 'before' => 'auth'), function()
{
    Route::controller('media', 'App\Controllers\Dash\MediaController');
});

但它仍然给我这个错误:

  

类'App \ Controllers \ Dash \ MediaController'未找到

1 个答案:

答案 0 :(得分:2)

除非您使用PSR-4(甚至是PSR-0),否则必须执行

composer dump-autoload 

每次在文件夹中创建新类。因此,作曲家将这些类添加到vendor/composer/autoload_classmap.php文件中。

看着你的PSR-0自动加载结构,我们可以看到你的控制器被设置为:

"App\\Controllers": "app/controllers",

所以现在作曲家自动加载系统会尝试在这个文件夹中找到你的App\Controllers\Dash\MediaController类:

/var/www/webapp/app/controllers/App/Controllers/Dash/MediaController.php

这很难,我知道,但是PSR-4更容易理解,因为

"App\\Controllers\\": "app/controllers",

表示您的控制器文件位于:

/var/www/webapp/app/controllers/Dash/MediaController.php