Laravel 5.5-ReflectionException(-1)类不存在

问题描述 投票:0回答:1

我正在尝试使用ReflectionClass从控制器获取文件名(从应用程序目录)。为了测试我是否可以使用ReflectionClass,我正在尝试以下方式:

在我的MyController.php中

public function readContent()
{

    $files = app_path() . DIRECTORY_SEPARATOR. "Drama.php"; 
    // It returns "F:\xampp\htdocs\projectDirectory\app\Drama.php"

    $class = new ReflectionClass($files);
    echo "file name:: ". $class->getFileName();
}

我在此路径中有一个Drama.php文件。但是,当我为此方法运行路线时,出现以下错误

ReflectionException (-1)
Class F:\xampp\htdocs\projectDirectory\app\Drama.php does not exist

我已如下更新我的composer.json文件:

"autoload": {
        "classmap": [
            "app",
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },

因此,我可以读取我的app目录文件。

我也运行了以下命令

  • composer dump-autoload
  • 作者更新
  • php artisan config:clear
  • php artisan cache:clear

但是我仍然收到此错误。谁能告诉我该如何解决?

php laravel laravel-5 artisan
1个回答
1
投票

您的问题是ReflectionClass将类路径作为constructor argument,而不是文件路径。请尝试使用new ReflectionClass('\App\Drama')

© www.soinside.com 2019 - 2024. All rights reserved.