Use phing to make Yii application "installable"

To use this "how-to", you need to install phing in your machine.

I want to show you a little example that allows me to configure gii password and database informations, with phing.

First of all, when I start a new yii application, I copy protected/config/main.php in protected/config/main.php.dist and updating some field like:

'db' => array(
    'emulatePrepare' => true,
    'charset' => 'utf8',
    'connectionString' => 'mysql:host=[[hostname]];dbname=[[database]]',
    'username' => '[[username]]',
    'password' => '[[password]]',

Also, I like to set gii configuration in this way:

'gii' => array(
    'class' => 'system.gii.GiiModule',
    'password' => '[[giipassword]]',
    'ipFilters' => array('', '::1'),

My complete config(.dist) file looks like this:


return array(
    'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
    'name' => 'YiiApp',
    'preload' => array(
    'import' => array(
    'modules' => array(
        'gii' => array(
            'class' => 'system.gii.GiiModule',
            'password' => '[[giipassword]]',
            'ipFilters' => array('', '::1'),
    'components' => array(
        'user' => array(
            'allowAutoLogin' => true,
        'db' => array(
            'connectionString' => 'mysql:host=[[hostname]];dbname=[[database]]',
            'emulatePrepare' => true,
            'username' => '[[username]]',
            'password' => '[[password]]',
            'charset' => 'utf8',
        'errorHandler' => array(
            'errorAction' => 'site/error',
        'log' => array(
            'class' => 'CLogRouter',
            'routes' => array(
                    'class' => 'CFileLogRoute',
                    'levels' => 'error, warning',
    'params' => array(
        'adminEmail' => '',

Now I can create build.xml and put it inside this content:

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyYiiApp" default="database">

    <target name="database" description="Configure database">
        <input propertyname="hostname" defaultValue=""
                promptChar="?">Enter the hostname of db.</input>
        <input propertyname="database" defaultValue="database"
                promptChar="?">Enter the name of db.</input>
        <input propertyname="username" defaultValue="root"
                promptChar="?">Enter username to access to the db.</input>
        <input propertyname="password" defaultValue="root"
                promptChar="?">Enter password to access to the db.</input>
        <input propertyname="giipassword" defaultValue="giipassword"
                promptChar="?">Enter password to access to gii.</input>
        <copy file="protected/config/main.php.dist"
                tofile="protected/config/main.php" overwrite="true">
                <replacetokens begintoken="[[" endtoken="]]">
                    <token key="hostname" value="${hostname}" />
                    <token key="database" value="${database}" />
                    <token key="username" value="${username}" />
                    <token key="password" value="${password}" />
                    <token key="giipassword" value="${giipassword}" />


And now, I can run

$ phing

The output will be similar to this:

Buildfile: /var/www/yiiapp/build.xml:

Enter the hostname of db. []? 
Enter the name of db. [database]? yiiapp
Enter username to access to the db. [root]? 
Enter password to access to the db. [root]? 
Enter password to access to gii. [password]? 
     [copy] Copying 1 file to /var/www/yiiapp/protected/config
     [copy] Copying 1 file to /var/www/yiiapp/protected/config


As you can see, some questions will be asked from you. Your answers will override main.php.dist file options when copying it to main.php.

This is just a simple example of how you can install your application using this method.

You can also, for example, create your own console command and call them with phing.

7 0
Viewed: 10 264 times
Version: 1.1
Category: How-tos
Tags: phing
Written by: sensorario
Last updated by: Maurizio Domba Cerin
Created on: May 22, 2012
Last updated: 6 years ago
Update Article


View all history