You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
Creating Yii applications with composer ¶
When creating web-application projects, you can add a great variety of different extensions, moduels, plugins or scripts to your code-base. But maintaining a stable combination and updating certain packages can become a hard task.
Luckily there are tools to handle the dependency management for you, like composer - which will be the package manager for Yii 2 - but this tutorial shows the integration of composer with Yii 1.1.
Prepare Application ¶
Before you start creating our Yii application with composer you have to do some preparation:
- download composer
- create
composer.json
file - create a class to handle composer callbacks
- create
config/console.php
While some of the details are described in the following paragraphs, you can download the example source-code from Phundament 3 yii-webapp branch.
Composer Setup ¶
Before using composer to retrieve packages for your application you have to adjust some values in composer.json
.
Repository ¶
Since Yii is not available via the standard packagist repository yet, disable the packagist repo for now and use the Phundament package repository, which has Yii framework and other Yii extensions available as a package.
"repositories": [
{
"packagist": false
},{
"type":"composer",
"url": "http://packages.phundament.com"
}
],
Packages ¶
If you want to create a basic Yii web application i.e., just add yiisoft/yii
to your requirements.
"require": {
"php": ">=5.3.2",
"yiisoft/yii": "1.1.*",
"yiiext/migrate-command": "0.7.1.1"
},
Note: because you'll have to manage migrations in different locations later on, also add the extension migrate-command
.
Callbacks ¶
You can use composer scripts to invoke Yii commands, first add some script hooks and an autoloading section for the callback class to composer.json
.
"autoload": {
"psr-0": {
"config": "./"
}
},
"scripts": {
"pre-install-cmd": "config\\ComposerCallback::preInstall",
"post-install-cmd": "config\\ComposerCallback::postInstall",
"pre-update-cmd": "config\\ComposerCallback::preUpdate",
"post-update-cmd": "config\\ComposerCallback::postUpdate",
"post-package-install": ["config\\ComposerCallback::postPackageInstall"],
"post-package-update": ["config\\ComposerCallback::postPackageUpdate"]
}
Hooks can be configured in config/console.php
as a parameter composer.callbacks
.
For details about the callback class, see its source-code.
Yii Configuration ¶
Now register specific callbacks, i.e. for Yii register a callback by the naming schema [vendor]/[package]-[action]
, which triggers the creation of a standard web application skeleton.
'params' => array(
'composer.callbacks' => array(
// args for Yii command runner
'yiisoft/yii-install' => array('yiic', 'webapp', dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'),
'post-update' => array('yiic', 'migrate'),
'post-install' => array('yiic', 'migrate'),
),
),
Note: There are callbacks for post-processing an update or install command. In this case, we've already registered the extended migrate command to run after install or update.
Now you're good to go and can start the installation with
php composer.phar install
Composer will now fetch the packages from composer.json
and because you've registered a callback for yiisoft/yii-install
it will not only download the code, but also invoke yiic
and create a standard web application skeleton at the given location.
Extend Your Application ¶
With the Phundament package repository you can already find some more ready to use extensions for your app. As an example you can add the popular yii-user
extension.
To install an extension configure the extension first.
Configure ¶
Adjust your configuration config/main.php
and config/console.php
according to the README instructions.
Add the module,
'modules'=>array(
'user'=>array(
'class' => 'application.vendor.mishamx.yii-user.UserModule',
),
),
update the database component (needed by yii-user)
'db'=>array(
'tablePrefix' => 'app_',
),
an update the migrate command (console.php only).
'commandMap' => array(
'migrate' => array(
'modulePaths' => array(
'user' => 'application.vendor.mishamx.yii-user.migrations',
),
)
)
Require ¶
Now you can add the package to your application with
php composer.phar require mishamx/yii-user
After the install process composer will trigger the post-install callback which now invokes the needed database migrations.
You can now use the module here http://yii-webapp/index.php?r=user
More… ¶
Have a look at Phundament 3 (master branch) for a Yii application with preconfigured modules for user, rights and media-management and add your own packages to packages.phundament.com.
Give feedback and join the development!
Packagist
Yii has been available in Packagist for quite a while: https://packagist.org/packages/yiisoft/yii
Quick instructions: Getting started with Yii and Composer
This article is quite extensive, but also a bit aimed at Phundament and it's components.
If you're just looking to get started quickly with Yii and Composer, try these instructions:
Prerequisites
Getting started
Run command to generate a new application:
~~~
yiic webapp myapp
~~~
Create a file
protected/composer.json
and insert the following content:{ "name": "myvendor/myapp", "require": { "php": ">=5.1.0", "yiisoft/yii": "1.1.*" } }
Open folder
protected
and run command to install dependencies:~~~
composer install
~~~
Open
index.php
and replace the following://$yii=dirname(__FILE__).'/../framework/yii.php'; $yii=dirname(__FILE__).'/protected/vendor/yiisoft/yii/framework/yii.php';
class not found
nice guide, can you help me with this repository? I imported it with the composer but after use it, return me "Class 'Brick\Geo\Engine\GeometryEngineRegistry' not found" repo here -> https://github.com/brick/geo thanks
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.