TailTemplate Build stunning websites faster with our pre-designed Tailwind CSS templates

Composer for beginner

Before Composer was invented, PHP developers struggled with package management despite the fact that we had PEAR. The fundamental design flaw of PEAR Package Manager made it hard to scale and most of developers abandoned it.

Composer is currently the default package manager for PHP developers. If you have not yet used Composer or feel you are not familiar with its basics yet, this article is for you.

When to use data provider

Before we dig into various Composer commands, it is essential to understand semantic versioning since that is what Composer uses to deal with packages' versions.

In summary, semantic version number follows the pattern as show below:

Major.Minor.Patch
  • An update in major part of the version number indicates that the package has an API change. This normally means it has introduced some incompatibilities, we should always run our tests to ensure it does not break our application.
  • An update in minor part of the version number indicates that some new features with backwards-compatibility are added. Which means it should not break anything other than adding something new.
  • An update in patch part of the version number indicates that there are bug fixes with backwards-compatibility. Which means it should not break anything, and we are encouraged to upgrade since there are bug fixes.

That is semantic versioning at a glance, you can certainly read more about semantic versioning at its official site.

Composer commands

With knowledge of semantic versioning in mind, it's time to get our hands dirty with Composer commands.

Install a package

To install a brand new package with Composer, we use the command as show below:

composer require cakephp/orm

We can also get very specific with a package's version. That is where semantic versioning comes to play. To install the exact version of a package, we do:

composer require cakephp/orm:3.0.0

To install specific range of version, we can use comparison operators such as >,>=,<,<= and !=. For example, to install the most recent version that is greater than 3.0.0, we can use the > operator:

composer require cakephp/orm:~3.0.0

To install a version with latest patches, but never introduce any minor releases, we can use the tilde. For example, to install the latest 3.0 version of cakephp orm without any minor releases, we can do:

composer require cakephp/orm:~3.0.0

The command above will install version that is >= 3.0.0 <3.0.

To install a version that never introduces a major release (breaking backward compatibility). We can use the caret. For example, to install latest cakephp orm version 3.0, we can do:

composer require cakephp/orm:^3.0.0

The command above will install version that is >=3.0.0 <4.0.0.

Update a package

From time to time, we may need to update all the packages, or update an individual package when there is an update.

To update all the packages, we can simply do:

composer update

To update a single package, we just need to append the package name instead:

composer update cakephp/orm

Remove a package

Composer also provides an easy way to remove a package from our project.

For example, to remove cakephp orm, we can do

composer remove cakephp/orm

The End

As the most popular package manager for PHP currently, Composer has enabled us to write distributable libraries with an ease. We hope you found this tutorial useful. If you have not used Composer yet, we highly encourage you start using it now.

Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.