starting an yii-based project

by prettyscripts on 2010-09-09 15:37

yii

note: v1.1.4 was used at the time of writing.

a workable yii project skeleton is created with command line:

/path/to/framework/yii webapp /path/to/project/web_root

there are certain aspect of the generated skeleton i don't like. such as the main project files under protected/ subdirectory is under the web root directory, which has internet presence. althought it is secured with .htaccess under that directory, the security could be compromised if not setup correctly.

i'm documenting the changes i make to the skeleton before starting working on the project.

create project path

create /path/to/project/yii/web. when creating the skeleton with command line, all files will be installed under this directory. when configuring web server, the domain should also point to this directory.

link the framework to yii/ subdirectory. on linux:

cd /path/to/project/yii
ln -s /path/to/yii/framework .

whenever a new version of yii is released, just delete framework and re-link the new framework here.

create the project

while still in yii directory, run the command line to create project:

framework/yiic webapp web

restructure the directories

we do not want the files in protected/ to have any web presence. move it to its parent directory, ie while still in yii/

mv web/protected .

edit path in index.php to reflect this change. the file should look like

PHP:

$yii=dirname(__FILE__).'/../framework/yii.php';
$config=dirname(__FILE__).'/../protected/config/main.php';

project environments

in general project development cycle, there should be a development, test and production versions.

i'm sure there are ways to distinguish these versions, but since i'm still not too familiar with yii, i opt for the simplest way.

there should be 3 main files to run for these environment:

  • index-test.php - this is generated in the skeleton
  • index-dev.php - copy this from index.php
  • index.php - delete any codes that's not meant for production.

the config files are in protected/config. there should be the following config files:

  • main.php - this is generated in the skeleton. we'll use this as the main config file for production.
  • test.php - this is also generated in the skeleton.
  • dev.php - copy this file from test.php.

now update the index*.php files in web directory with these config file name.

configuring gii

gii should only be run in dev environment. remove any references to gii in config/main.php and add gii module to dev.php.

if you're not running from localhost, see my previous post on how to config gii to run from outside localhost.

and now you can start working on your project!

i hope you'll find this article useful.

please note that i'm fairly new to yii. if you find anything not done correctly, please let me know the correct way to do it. thanks!