Own project through the composer

I have some libraries loaded through the composer and I wonder if it is possible to add my own library to the / vendor card and then load the composer loader? The structure will look like / vendor / mylibrary / and then the mylibrary namespace.

Is it possible? Could you also add another map for the composer autoloader? For example, for example / app / src /, and then load all the classes in this folder? Or do I need to make my own bootloader for this?

thanks

+8
php composer-php
source share
3 answers

Reading composer documentation:

You can even add your own code to the autoloader by adding an autoload field to composer.json.

{ "autoload": { "psr-0": {"Acme": "src/"} } 

}

The composer will register the PSR-0 autoloader for the Acme namespace. You define a mapping from namespaces to directories. The src directory will be in your project root directory at the same level as the vendor directory. An example of a file name would be src / Acme / Foo.php containing the Acme \ Foo class.

After adding the startup field, you need to restart the installation in order to recreate the vendor / autoload.php file.

So, you just need to follow PSR-0 and tell the composer where to find your library by adding this line to your .json composer

+7
source share

Yes. You can achieve this. Configure the composer.json file as follows:

 { "autoload": { "classmap": [ "classes" ] } 

Here, classes is the name of the directory in which you have all the classes associated with your application. A class associated with it should also be automatically detected. Just add the following line to achieve both at the same time:

 require 'vendor/autoload.php'; 

And you can use namesapce to reference your class as follows:

 use classes\Model\Article; 
+2
source share

Yes, of course, you can add your own libraries, and you should be very pleased to do so. If your library is publicly available, you can simply register it at packagist.org. If not, it is a little more difficult, but not impossible.

If your project does not comply with the PSR-0 standard, the composer will create a class map for you. Custom autoloader is not supported.

I would recommend you read the (really excellent) documentation about all this and come back if you have problems.

http://getcomposer.org/doc/

+1
source share

All Articles