Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSR-4 autoloading with Composer

Tags:

I run a portail with composer's autoloading class system:

"autoload": {
    "psr-4": {
        "Portal\\": "src/"
    }
}

It works when I run composer.phar dump -o, for instance my class Boostrap is well referenced into vendor/composer/autoload_classmap.php file:

'Portal\\Core\\Bootstrap' => $baseDir . '/src/core/Bootstrap.php',

But when I don't run the optimized option on autoload dumping, the autoloading system doesn't works anymore:

Fatal error: Class 'Portal\Core\Bootstrap' not found in /var/www/portail/prod/web/index.php on line 7

How can I make autoloading works without -o option?

like image 936
Fractaliste Avatar asked Feb 19 '15 13:02

Fractaliste


People also ask

How do you autoload with Composer?

After you create the composer. json file in your project root with the above contents, you just need to run the composer dump-autoload command to create the necessary autoloader files. These will be created under the vendor directory. Finally, you need to include the require 'vendor/autoload.

What is PSR 4 autoloading standard laravel?

Overview. This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.

How do I get the Composer JSON file?

To configure Composer for your PHP app json file specifies required packages. Verify that a composer. json file is present in the root of your git repository. Run composer install (on your local machine) to install the required packages and generate a composer.

Where is Composer json located?

composer. json is a JSON file placed in the root folder of PHP project. Its purpose is to specify a common project properties, meta data and dependencies, and it is a part of vast array of existing projects.


1 Answers

There are two ways to fix it.

  1. change composer.json to

    "Portal\\Core\\": "src/core/"
    
  2. Or rename the core directory to Core

https://getcomposer.org/doc/04-schema.md#psr-4

The subdirectory name MUST match the case of the sub-namespace names.

http://www.php-fig.org/psr/psr-4/

like image 61
sectus Avatar answered Sep 17 '22 15:09

sectus