简体   繁体   中英

Laravel 5.5 - ReflectionException (-1) Class does not exist

I am trying to use ReflectionClass to get file name(from app directory) from a controller. For testing whether I can use the ReflectionClass or not i was trying in following way:

In my 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();
}

I have a Drama.php file in this path. But when I am running the route for this method, I get following error

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

I have updated my composer.json file like following:

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

So that, i can read my app directory files.

I also ran the following commands

  • composer dump-autoload
  • composer update
  • php artisan config:clear
  • php artisan cache:clear

But i am still getting this error. Can anyone tell me how can i resolve this?

Your issue here is ReflectionClass takes the class path as the constructor argument , not file path. try new ReflectionClass('\App\Drama') instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM