【问题标题】:Laravel simply won't find my classLaravel 根本找不到我的课程
【发布时间】:2014-09-13 21:52:41
【问题描述】:

Laravel 一直告诉我找不到我的课程,我很确定我已经用尽了任何解决方案。

路线:

Route::get('/custom-reports/timezone', array( 'uses' => 'Controllers\Reports\TimezoneController@generate'));

目录:

app/controllers/Reports/TimezoneController.php

TimzoneController.php

<?php 
namespace Controllers\Reports;
use Controllers\BaseController;

class TimezoneController extends BaseController
{
    public function generate() {}
}

作曲家.json:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/libraries",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/libraries/Illuminate/Cookie/Guard.php"
    ],
    "files": [
        "app/helpers.php"
    ]
},

错误:

ReflectionException -1
Class Controllers\Reports\TimezoneController does not exist

我已经做了php composer dump-autoload

【问题讨论】:

  • 尝试将目录名称重命名为 Controllers 并运行转储

标签: laravel


【解决方案1】:

classmap 不支持命名空间(据我所知),因此您需要设置 PSR-4 自动加载器。

将您的composer.json 更改为此并运行composer dump-autoload。之后它应该可以工作了。

"autoload": {
    "classmap": [
        ...
    ],
    "files": [
        ...
    ],
    "psr-4": {
        "Controllers\\": "app/controllers"
    }
},

【讨论】:

  • 您的答案有效,但我的应用程序中有几个其他目录和类在此结构中工作,无需添加 psr-4 位。不确定它们是如何正常工作的。 :L
猜你喜欢
  • 2014-12-19
  • 2015-07-27
  • 2015-01-04
  • 2016-12-02
  • 2012-08-20
  • 1970-01-01
  • 2011-12-13
  • 2017-01-20
  • 1970-01-01
相关资源
最近更新 更多