Mastering Composer - Tips and Tricks

Originally published at: http://www.sitepoint.com/mastering-composer-tips-tricks/

Composer has revolutionized package management in PHP. It upped the reusability game and helped PHP developers all over the world generate framework agnostic, fully shareable code. But few people ever go beyond the basics, so this post will cover some useful tips and tricks.

Global

Although it’s clearly defined in the documentation, Composer can (and in most cases should) be installed globally. Global installation means that instead of typing out

php composer.phar somecommand

you can just type out

composer somecommand

Continue reading this article on SitePoint

There is also the Scripts attribute where you can specify commands to be run before/after a command. Example from Laravel composer.json:

"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-create-project-cmd": [
        "php -r \"copy('.env.example', '.env');\"",
        "php artisan key:generate"
    
    ]
}

Indeed, forgot that one, thanks!

What is that list of dependencies for? Do you actually have an environment that uses all of them? smiley

Scott

Yeah, most of those were actually pulled in by ezPublish.

One can always check what releases are valid for a certain version constraint by using semver.mwl.be.

1 Like

I think composer.lock should always be commited. Today's techniques for deployment rerun composer install on each deploy which on a large composer.json (without composer.lock) file can result into a lot of time waiting.

As you do an intro in composer usage, you should drop a line on composer init way to create initial composer.json
I like your writing Bruno, thanks

1 Like

Would be good to comment that composer require does not actually need you to specify the version and is also recommended that you let composer decide, as it also means you do not need to go look that up.

composer require package will automatically insert something like ~1.11 and will tell you. Adding the version only needs to be done in less frequent cases where you need to specify a version other then the latest.

Thanks, pointed it out additionally in case it wasn't clear from the code snippet.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.