How to attach composer to Yii project
There's a good manual about how to attach composer to anything. This is a detailed manual about how to attach composer to existing project based on Yii1 framework.
Download composer
It's good to install composer globally but you can't be sure it's installed on all servers where you project can be.
So it's a good idea po put composer.phar
file inside your app. To do that run this line:
curl -sS https://getcomposer.org/installer | php
Require something
As a sample you can require yii framework code with line like this:
php ./composer.phar require yiisoft/yii 1.1.15
That will generate composer.json
and composer.lock
files and vendor/
dir.
composer.json
and composer.lock
files should be stored in your git index.
vendor/
dir should be ignored:
echo "/vendor" >>.gitignore
Setup autoloading
Composer cares about autoloading. To enable it you should call a line like this:
require __DIR__ . '/vendor/autoload.php';
You should require this autoload before requiring yii framework code.
There are 3 entry scripts in simple yii app: index.php
, protected/yiic.php
, protected/tests/bootstrap.php
.
Hide everyhing
If vhost points to your app root directory - you should restrict access to composer files.
Code like this in .htaccess
will redirect all requests to these files to index.php
so they are handled by your app.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} vendor/ [OR]
RewriteCond %{REQUEST_FILENAME} composer.phar [OR]
RewriteCond %{REQUEST_FILENAME} composer.json [OR]
RewriteCond %{REQUEST_FILENAME} composer.lock
RewriteRule . index.php [L]
Profit
BTW, it's already attached to cleanapp application template.
And it's attached by default to all Yii2 app templates.