You are viewing revision #4 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
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('127.0.0.1', '::1'),
),
My complete config(.dist) file looks like this:
<?php
return array(
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
'name' => 'YiiApp',
'preload' => array(
'log'
),
'import' => array(
'application.models.*',
'application.components.*',
),
'modules' => array(
'gii' => array(
'class' => 'system.gii.GiiModule',
'password' => '[[giipassword]]',
'ipFilters' => array('127.0.0.1', '::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(
array(
'class' => 'CFileLogRoute',
'levels' => 'error, warning',
),
),
),
),
'params' => array(
'adminEmail' => 'webmaster@example.com',
),
);
Now I can create build.xml and put it inside this content:
[xml]
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyYiiApp" default="database">
<target name="database" description="Configure database">
<input propertyname="hostname" defaultValue="127.0.0.1"
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">
<filterchain>
<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}" />
</replacetokens>
</filterchain>
</copy>
</target>
</project>
And now, I can run
$ phing
The output will be similar to this:
Buildfile: /var/www/yiiapp/build.xml:
Enter the hostname of db. [127.0.0.1]?
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
BUILD FINISHED
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.
User Contributed Notes 3
Filterchains
Hello there,
first of all - nicely done. I'm just curious why you've used the replacetokens filter instead of expandproperties?
There is no real reason
My goal was to fast update my config file. Tell me: wich one is better, and why? For me replacetokens is more intuitive.
Nice one
I've never used it before but this seems to be a pretty cool "phing" ;-) Especially if you have an app that's used in many projects and that uses a few customized parameters (app-name, admin-email etc.). Thanks for sharing
Leave a comment
If you have any questions, please ask in the forum instead.
Join the conversation to share a note.