Composer has become the most used dependency manager for PHP. While some people still use PEAR for other reasons, composer has been even widely adopted thanks to all the packagist available packages. Composer solves the problem of reinventing the wheel allowing you to use multiple third party open source libraries for PHP without caring about the requirements or dependencies of the installed library, as composer will do it automatically for you.
Old projects based on Symfony 1.4 were usually distributed using a .tar
or .zip
file, so you can't install a composer library quickly. However, it's possible to do it and we'll show you how in this article.
Requirements
In order to implement composer on the legacy Symfony 1.4 using this article, you will need basic knowledge of Composer (how it works), composer installed and obviously a demo project to test it.
1. Init composer on your project
The first thing you need to do with every project that uses composer, is to create the composer.json file on the root of your project. You can create it interactively with the command line using the following command:
composer init
Or alternatively, creating it manually with at least the following structure:
{
"name": "ourcodeworld/projectname",
"type": "project",
"authors": [
{
"name": "Your Name",
"email": "[email protected]"
}
],
"require": {
}
}
Note that till this point there is no autoloader file that you can include in your project nor composer.lock file. To create the autoloader, you will need to install the first library that you need.
2. Install some library to test
As next basic step of the implementation of composer in your project, is simply enable the autoloader.php file of composer and that would be enough. However, before enable this, the file needs to exist ! That's why you need to install some library first in order to create the mentioned file. In this case, as shown in the official tutorial of Composer, we are going to install the monolog package on our project:
Important
Remember to install the version of the library that you want to use that is compatible with the PHP version that your Symfony 1.x needs e.g PHP 5.3.3.
composer require monolog/monolog
This will create the /vendor
folder in the root folder of your project where all the libraries will be placed after the installation, here you will find the autoloader as well.
3. Register composer autoloader on your project configuration
Now that the autoloader file of composer exists, you can include it in your project configuration file (yourproject/config/ProjectConfiguration.class.php
) using require_once
:
<?php
require_once dirname(__FILE__) . '/../symfony-1.4.20/lib/autoload/sfCoreAutoload.class.php';
// In this file register the autoloader of composer using the following line:
require_once __DIR__.'/../vendor/autoload.php';
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration {
public function setup() {
// Your original code
}
}
After that, clear the cache using php symfony cache:clear
and try to use the new library from any controller, model, form on your Symfony project
Happy coding !