Running your Yii app and configuring it
December 29th, 2008
Unlike in CakePHP, it is mandatory to use the shell tool (called yiic, similar to CakePHP’s “Bake” script) to start off your application as it creates the workspace architecture (The Yii package does not come with an application bare-bones, only framework code). Yiic generates .htaccess files with rules that forwards user requests to index.php where the application runs (cake does this too). When I checked my index.php file to see what was in it, I was amazed with the possibilities of what I saw.
<?php // change the following paths if necessary $yii = 'C:\wamp\www\yiiRepo\framework\yii.php'; $config = dirname(__FILE__) . '/protected/config/main.php'; // remove the following line when in production mode defined('YII_DEBUG') or define('YII_DEBUG',true); require_once($yii); Yii::createWebApplication($config)->run();
The last two lines are the important ones. In the second-last line, it includes the main Yii class file, so that it can actually run the application as seen in the last line. createWebApplication() accepts one argument: the path to the configuration file for the application. In the configuration file you can configure things such as your database, error and mysql logging, and url routing. The configurations are stored in the form of an array. It’s a much simpler approach than cake’s, which uses a heavy configuration class. Personally I am liking Yii’s array configuration syntax much better than cake’s (I will show you my Yii configuration file in a second).
Since in Yii you define the configuration file used in your index.php file, you can greatly simplify the synchronization between your development and production server – you simply need different index files and separate configuration files. So I thought up a inheritance/tree system for configuration files.
/protected /config /main.php /production.php /development.php
Here, the production and development configuration files will simply extent the main configuration file which holds the configurations that are consistent across both production and development servers such as the url routing rules. Possible examples of each file:
protected/config/main.php:
<?php // This is the main Web application configuration. return array( 'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name' => 'My Web Application', // Tell's Yii is can auto-load model and component classes on-demand (lazy loading) 'import' => array( 'application.models.*', 'application.components.*', ), 'preload' => array('log'), //preload the log component right away (eager loading) // application components 'components' => array( 'user' => array( // user component settings here (similar to CakePHP's auth component) ), //log class can output errors, mysql quarry summaries, etc. The specific //settings are extended in the production and development config files 'log' => array( 'class' => 'CLogRouter', ), 'urlManager' => array( 'urlFormat'=>'path', 'showScriptName' => false, 'rules' => array( // some generic url routes I often use 'user/register/*' => 'user/create', 'user/settings/*' => 'user/update', ), ), ), );
protected/config/development.php:
<?php //import main configurations $main = include "main.php"; // Development configurations $development = array( 'components' => array( 'db' => array( 'class' => 'CDbConnection', 'connectionString' => 'mysql:host=localhost;dbname=yiitestdrive', 'username' => 'root', 'password' => '', ), 'log' => array( 'routes' => array( //set the log component to show logs on the bottom of the page - similar to CakePHP array( 'class' => 'CWebLogRoute', 'levels' => 'trace, info, error, warning', 'categories' => 'system.db.*', ), ), ), ), ); //merge both configurations and return them return CMap::mergeArray($main, $development);
protected/config/production.php:
<?php //import main configurations $main = include "main.php"; // Production configurations $production = array( 'components' => array( 'db' => array( 'class' => 'CDbConnection', 'connectionString' => 'mysql:host=mysql@example.com;dbname=productionDatabase', 'username' => 'admin', 'password '=> 'password123', ), 'log' => array( 'routes' => array( // Configures Yii to email all errors and warnings to an email address array( 'class' => 'CEmailLogRoute', 'levels' => 'error, warning', 'emails' => 'admin@example.com', ), ), ), ); //merge both configurations return CMap::mergeArray($main, $production);
As you can see, in the main config file we have the consistent settings such as URL routing, and in the production and development files we have inconsistent things such as database settings. Note also in the production config I have errors and warnings sent to an email while in the development config I have all logs outputted at the bottom of the page. Nifty, huh? Now we can set up the index files for the production and development servers like so:
Development index.php file:
<?php $yii = 'C:\wamp\www\yiiRepo\framework\yii.php'; $config = dirname(__FILE__) . '/protected/config/'; // turns debug on defined('YII_DEBUG') or define('YII_DEBUG',true); require_once($yii); Yii::createWebApplication($config . 'development.php')->run();
Production index.php file:
<?php $yii = 'framework/yii.php'; $config = dirname(__FILE__) . '/protected/config/'; require_once($yii); Yii::createWebApplication($config . 'production.php')->run();
Now you can easily set different settings for production and development set-ups without redundancy. Enjoy.
UPDATE: Qiang wrote his own version of this method in the Yii Cookbook here. He recommends you merge the configurations with CMap::mergeArray() instead of array_merge_recursive(). I have changed this article to use CMap also. CMap::mergeArray() is a helper method that comes from the Yii core.
Categories: Yii




Very nice trick and writing! You should share this with the Yii community.
Thanks Qiang! Glad to have your approval (as a Yii developer). But I am in fact sharing this with the Yii community, aren’t I, by blogging it? Do you want me to put it on the forum? I wouldn’t mind advertising my blog on the forum if that’s ok.
Nice job. I’m in the learning curve of Yii myself. If you place the framework directory relative to application, you could set the $yii variable with a dirname as well.
Thanks for the write ups so far. I’m exploring frameworks right now and trying to decide on one to use. I don’t really have any experience with them, so I’m looking forward to more of your thoughts on Yii.
Excellent information!
wow. thanks)
Thanks, excellent tips!
was very helpful for me, just a few corrections:
you forgot the semicolon behind CMap::mergeArray(..)
and ‘CEmailLogRoute’ requires ‘emails’=>’blabla’ as argument not ‘email’
Fixed, thanks!
Excellent Tutorial, I just started read in the web world about Yii and after i saw ur two excellent posts i am gonna stick with yii after a month of looking for the best php framework.
I hope u provide us with excellent and nice step by step tutorial.
Thanks
Hi, I propose something like that on the index.php (main) file:
[code]
run();
[/code]
So just change APPLICATION_ENV constant to your needs :-)
I dont know why, but my code above dont apears, so I create a pastie: http://pastie.org/1522272
Great.
Thanks
I’m a member of a gym wide6
49971
OPLURUZZRO uggs france CVCWCGQYAG http://www.ne-ns.com
zbqtbqiq.uipvhiut, titanium trend micro, PMWMHaU, [url=http://1antiviruszone.com/]trend micro titanium antivirus 2011 free download[/url], NIoqWGn, http://1antiviruszone.com/ trend micro titanium antivirus 2011 free download, lbpgRzO.
pkjfjqiq.uipvhiut, Paul Mitchell Flat Iron, ZIDigyt, [url=http://paulmitchellflatiron.com/]Paul Mitchell Hair Straightener[/url], ZnOCwdB, http://paulmitchellflatiron.com/ Paul Mitchell Hair Straightener, rGxIliM.
jpqlkqiq.uipvhiut, Valium without a prescription, brfDXxU, [url=http://www.freespiritparagliding.com/]Generic Valium[/url], VkmTvop, http://www.freespiritparagliding.com/ Valium, delyQZL.
euhsoqiq.uipvhiut, diet pills, YwLcokE, [url=http://www.weightlossfortoday.com/]weight loss products[/url], jIlsdxO, http://www.weightlossfortoday.com/ diet pills, PkAKvRY.
iqxptqiq.uipvhiut, House Prices, TjuTiXb, [url=http://sustainable-finance.org/]House Prices[/url], giNRQRs, http://sustainable-finance.org/ House Prices, UfpVfFE.
qwtrxqiq.uipvhiut, business lead generation, UBJnLTP, [url=http://myleadnetproonline.com/]lead generation[/url], HzDkzkp, http://myleadnetproonline.com/ lead generation, hNPOOjh.
What sort of work do you do? Lolitas Rompl
qxx
myrqyqiq.uipvhiut, Purchase ambien no prescription, VjzWLCB, [url=http://www.celebrityhotspot.com/]Buy ambien discount[/url], pzDCklZ, http://www.celebrityhotspot.com/ Buy Ambien, BNxDVew.
epiveqiq.uipvhiut, Purchase valium online, pYRQgbU, [url=http://www.passportvisapros.com/valium.html]Buy online no prescription order valium[/url], SufLrfb, http://www.passportvisapros.com/valium.html Buy online no prescription order valium, mXMZrby.
Could I ask who’s calling? Lolita Hentai
8(
I came here to work Preteen Girls Nn
475535
albaoqiq.uipvhiut, Best Dating Sites, xWmSRGH, [url=http://top10bestonlinedatingsites.com]Online Dating Sites[/url], lxPYweE, http://top10bestonlinedatingsites.com Best Online Dating Sites, YZXqHjC.
kguqrqiq.uipvhiut, Zetaclear nail fungus treatment, tSqOHgd, [url=http://zetaclear4u.com/]ZetaClear[/url], THsKupM, http://zetaclear4u.com/ ZetaClear, cJZzwtu.
icwemqiq.uipvhiut, Porn videos, ZtioYrp, [url=http://hteps.com/]Hentai porn[/url], lNfCfao, http://hteps.com/ Lolita porn, AzBZinZ.
We’ve got a joint account Freedom Lolita Bbs
aseo
I’ve been cut off Preteen Sex Stories
:((
vrztsqiq.uipvhiut, speed up my pc, XvgDRAI, [url=http://www.speedupmypcfree.net/]speed up my computer[/url], PjPmUUN, http://www.speedupmypcfree.net/ speed up my pc, VQxOBpC.
A pension scheme death wish rape pictures >:-((( lesbian hardcore porn and rape 2670 brutal rape sex >:]]] strabon rape 67627 rape in the holocaust obui karate girls vs rape team 98797 rape scene young actress 949434 statutory rape definition :-( cartoon rape pron :[ abduction rape movies =DD
WJUXNXorAINODNT cheap uggs BTDORKndRXLRJOK http://cheapuggsuksale.webeden.co.uk
SFGPIGTKHF cheap tiffany jewelry LGBFIBOKBA http://www.brooklynsunsetweb.com
It’s a bad line hentei rape fantasy %DD dad son sex rape incest 58106 free rape top 100 >:] teen date rape research 918333 forced feminization forced feminization stories :[ rape incest 2 yafm stargirl rape rpg hjqx her ass rape 83255 fantsy rape stories =-) tennessee stautory rape %-)))
evyhiqiq.uipvhiut, Stickam caps bate, SWmMNAd, [url=http://funadultcams.com/]Jailbait stickam capture[/url], UkaXyBD, http://funadultcams.com/ Stickam tits, FaBcBGJ.
Do you know the number for ? Pthc Lolita Top
ivte Lolita Preteen Nymphets
4119 Bbs Preteen Models
%[
I’m not interested in football Top Lolita
ourxl Preteen Nonude
%]]] Lolita Art
404106
Have you got a current driving licence? Preteen Bikini Models
>:[[ Preteen Models Bbs
pdw Lolita Incest
:))
Whereabouts are you from? Preteen Vagina
jote Preteens Nude
:-((( Preteen Lolita Pussy
%]]]
I was made redundant two months ago Gothic Lolita
135681 Lolita Boys
558 Bbs Lolita
:P
I’d like to take the job Preteen Lolita Top
>:PP Lolita Forum
048 Lolitas Top 100
eio
It’s serious Underage Lolita Pic
45607 Preteen Nude Pics
406 Preteen Underage Nude
414385
We used to work together Lolita Underage Nude
849 Lolita Cumshots
:-))) Preteen Lolitas
>:(
What sort of music do you listen to? lolitas nudes pics foto 004 lolita sex bbs guestbook 824915 14 lolita free 968 lolita underage 12 akyc girl hot sex lolita roapwz lolitas vodeo 1487 lolitas messageboard 7941 lolitas lesbian free 063 nude kids lolitas %-DD top lolitas link >:[
I’m unemployed 101 lolita sex sites =-DDD child pedo lolitas :]]] preteen lolita fuck kids igho lolita blog forum 6203 lolita nude teen jmgfbu lolita video clip llyjab lolita porn pedo preteen rtgn preteen toplist nude lolita lxpkmr wild lolita
oftcm sick russian lolitas ejcsu
Have you seen any good films recently? preteen models bbs lolita =-O small lolita vagina ewlnc preteen lolita xxx free 51245 cp lolita movie
vrrpi shylolita top dark bxb xxx russian lolitas pedo %]] lolita bbs kds video 8-[ lolita eat cum aroud bbs dragon lolita 831311 gallerie virgin porn lolita 337
Do you need a work permit? fucking kid russian fzvenn
xilouqiq.uipvhiut, fqdzandmsm
txodtqiq.uipvhiut, Viagra, WMbeKDI, Free ares 2 0 9 download mac software wareseeker search, lIwxSut, Meninas, BAEDtup, Free online blackjack games, eDTzOIj, Torrent Downloads, hPhnEAY, Juegos de carros de carrera y auto bus, LEaWCJq.
gcxtxqiq.uipvhiut, Mapquest, JGXYLDg, Life drawing model photos, pvQizxS, sell house quick, SfwSNrO, Created by awstats plugins online pharmacy tramadol, fFyZkHO, african mango australia, sZATfBa, Tagesgeld Zinsen, rUgSFFw.
prrbsqiq.uipvhiut, Buy ambien online pharmacy, JEFBFvZ, Where to buy sizegenetics, tmuMbZS, siteground web hosting, aqyxBwp, Buy xenical online no prescription, lZTGXeZ, Ultram clonazepam, fSSgmCN, vps cpanel hosting, BmjFGpd.
qgnriqiq.uipvhiut, Breast actives before and after pictures, SjoyvBl.
Walt Whitman: “I celebrate myself, And what I assume you shall assume, For every atom belonging to me as good belongs to you.”