Composer dump-autoload gives preg_match error

545 views Asked by At

I have Composer in my PHP project installed, and want to use the autoloader. On this page I read how the composer.json file should look like and that I should run the command dump-autoload. My composer.json file looks as follows

{
    "require-dev":{
        "phpunit/phpunit":"4.5.*",
        "autoload":{
            "psr-0":{
                "Yii\\":"yii-1.1.14.f0fee9/"
            }
        }
    }
}

When I run from the terminal php composer.phar dump-autoload, I get the following error message.

Error message Composer

  1. What second parameter and what array is it talking about?
  2. How can I solve this issue to generate a new autoload.php file?
2

There are 2 answers

0
PiranhaGeorge On BEST ANSWER

autoload should be moved out of require-dev:

{
    "require-dev":{
        "phpunit/phpunit":"4.5.*"
    },
    "autoload":{
        "psr-0":{
            "Yii\\":"yii-1.1.14.f0fee9/"
        }
    }
} 

You can test your composer.json file using composer validate. Your original file returned:

./composer.json is invalid, the following errors/warnings were found:
require-dev.autoload : invalid value, must be a string containing a version constraint
the property name is required
the property description is required
No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
0
Blaatpraat On

Your composer.json file is not correct.

require-dev and autoload are 2 main sections.

This should work:

{
    "require-dev":{
        "phpunit/phpunit":"4.5.*"
    },
    "autoload":{
        "psr-0":{
            "Yii\\":"yii-1.1.14.f0fee9/"
        }
    }
}