If you haven’t already heard of it, Composer is a dependency manager for php and has really made an impact in the PHP development community. It brings the same advantages as Node’s NPM and Ruby’s Gem package managers to the PHP world. Lots of other frameworks like Laravel use it to manage their core files and plugins. There are also other projects to try and bring Composer management to products like Wordpress and with just a little modification we can try to bring some of these same advantages to Opencart.
Use in Opencart
Unfortunately at the time of writing Opencart does not support Composer out of the box. I have added a suggestion to their uservoice, so if you think its a good idea please vote for it! However we can quite easily integrate composer with opencart and there is already an extension for 188.8.131.52 that does just that.
If you want to do it yourself, you just need to include the autoload.php file somewhere in the Opencart startup. This is the vQmod file we usually use:
That’s it and now you can start to require packages for your project. First thing is to install Composer and create a composer.json for your project. Here is a sample one to get you started:
Packages to install
There are lots of packages to choose from. However as a starter here are some we use regularly in our Opencart installations.
Whoops / Sentry
Opencart’s error handling leaves a lot to be desired and we love the Whoops library. We also use Sentry to report on PHP errors in production. So we created a repo that shows how you can integrate Whoops and Sentry into opencart with Composer.
Another issue with Opencart is that it doesn’t have any built in support for database migrations. However there is a nice stand alone package called Phinx that lets you create and manage migrations. All you need to do is add phinx as a dependency in your composer.json:
Then you can run
php vendor/bin/phinx init to create your phinx.yml file and add in your database info.
Check the documentation for details on how to write and run migrations.
Please be aware that by default Opencart does not block access to yml files. So make sure your database details are not available to the public!
Hopefully we have inspired you to see what other packages you can pull into opencart with Composer and maybe you will be able to enhance your Opencart project. There are lots of Composer repositories where you can search for and download packages available but the main one is Packagist.