3 ms·
I read through this post and spent the morning reading more about Composer. I've been on the fence for a while, but I'm still not sure yet if this is right for
by RpFLCL 8y ago
I read through this post and spent the morning reading more about Composer.
I've been on the fence for a while, but I'm still not sure yet if this is right for me.
Most of the projects I work on have minimal dependncies & existing autoloading strategies. While I would like to have access to new dependencies via Composer it seems like I'll be forced to engage in heavy refactoring to update namespaces and will need to somehow package my existing code.
Curious if anyone here has firsthand experience adding Composer to an existing project that wasn't dependency heavy?
- tylerjwilk00 8y agoYou won't even notice it. Everything you already have will continue to work fine. All you have to do is include the autoloader somewhere in your code path. An example assuming you already installed composer on the machine into your /bin or similar directory. # add a new dependency: composer require guzzlehttp/guzzle # this dependency will now be in the vendor directory ls ./vendor # update your apps entry point (ie: frontcontroller.php): // require composer's autoloader require ('vendor/autoload.php'); # autoload will now find any dependencies you add via composer // create guzzle client $client = new \GuzzleHttp\Client(); # another tip, if you already have a bunch of class files some where (ie: myapp/src/classes , myapp/src/models ) You can include class paths via composer.json "autoload": { "classmap": ["classes/", "models/", "MyClass.php"] } You can now remove any hard-coded include or require statements. There is a lot more things you can do with it but this is the most basic example.
- krapp 8y ago>There is a lot more things you can do with it but this is the most basic example. One useful feature is custom commands[0]. [0]https://getcomposer.org/doc/articles/scripts.md#writing-custom-commands https://getcomposer.org/doc/articles/scripts.md#writing-cust... Also, regarding autoloading, I find that once you get used to the structure of PSR-4, adding files to a PSR-4 namespace[1] and just having autoloading work is incredibly helpful. The only pain point is dealing with namespaces everywhere. [1]https://getcomposer.org/doc/04-schema.md#psr-4 https://getcomposer.org/doc/04-schema.md#psr-4