Probably one of the most important principles in Software Engineering is DRY (Don’t Repeat Yourself!), every time you found yourself writing the same code more than once, in a same project, or in different projects then you have to re-think of what you have done. For example, how many times you have wrote authentication system for your projects? Or email class to send emails with PHP? If you’re doing it over and over again, maybe a package management could help you on this.
PHP Packages Dependencies Manager
PHP has PECL and PEAR as repository for extension and code libraries, PECL stands for PHP Extension Community Library, in PECL extensions is written in C language, and can be loaded into PHP to provide additional functionality. What you need to use PECL is administrator rights, C compiler and associated dependencies for those extensions. PEAR is PHP Extension and Application Repository, where in PEAR the extension or libraries is written in PHP. So it could be downloaded and loaded to our PHP code.
PECL and PEAR are great tools, but they are system-wide packages manager, meaning, it’s installed on the system not per project, and not everyone prefer that, at least me. I often just do everything on-the-fly in PHP, whenever I need a PHP library, I will just simply download and include it, and it’s the era of PHP Frameworks, many commons functionalities has been included on the frameworks. But that’s create one big problem, everyone have their own pick on the framework, and each framework has their OWN implementation of providing functionalities with their libraries, that makes switching frameworks not an enjoyable task, we need to re-learn many things.
What is Composer?
Composer exists to overcome the problem mentioned above, composer is a dependencies manager that come in a form of command utilities, composer make it possible to ‘plug and play’ PHP packages across application no matter what frameworks we are using as long as the packages follow the shared format. With composer we can define our project dependencies simply in a ‘composer.json’ file, and later on we can install and update these dependencies automatically.
This will make composer include ‘blade’ from the vendor `illuminate` in our project, and we can do update to the ‘blade’ package with one command.
Setting Up Composer In Windows
And here comes the main topic, how to set up composer, and this one is specifically written for windows users. At first, I planned to wrote the complete instruction on how to setup composer in windows with the standard way, which including downloading the installer manually and run the install on command prompt, and also how to make the composer can be accessed globally by pre-run a bat script every time we open command prompt, it can be achieved by hacking around with the registry editor, but I found a much better solution, instead of going with all the fuss, we are going to use a really cool installer built by John Stevenson which you can download the composer windows installer and do the install easily. With this installer, you can choose whether you want to install it per project basis or install it globally so you can run the ‘composer’ command everywhere on your projects.
Installing Composer in Per Project Basis
To install composer in windows per project basis, you just need to choose the ‘basic’ option in the installation procedure.
Composer Windows Installer Basic Option
And then you just need to specify the folder, where you want the composer will be installed.
Composer Windows Installer Choose Folder
And here it is, you can access the composer on the selected folder, and you will need to be able to run ‘php’ via command prompt, and use the composer.phar that has been installed in the folder.
Run Composer Command Per Project Basis
Installing Composer to Use Globally in Every Projects
This is the setup that I prefer, to install the composer globally, that way you can use composer everywhere. And to setup composer in windows globally, just like the previous one, you just need to choose an option, which is ‘global’ option.
Composer Windows Installer Global Option
And here you go, open up your command prompt, and you can access composer, as easy as just calling
Run Composer Command Globally
Composer is a great tool for managing packages and dependencies in your projects, let’s hope every packages in PHP will be available in composer soon and I encourage everyone to write their packages in compliance with composer, so that everyone can share their packages no matter what framework we are using. And for your information most of the famous PHP frameworks has started to give their full support to composer, and built their system components as composer packages, you can check out FuelPHP, Laravel 4, and Symfony2. So it’s the end of guide on setting up composer in windows, for UNIX based operating system, you could do this easily via terminal, which has been documented well on the composer site.
Reposted from http://diywebdev.com/setting-up-composer-php-dependencies-manager-in-windows/