
Initializing the project
With our final database structure figured out, we can now start writing code. Using the instructions in the official guide ( Yii is installed, navigate to your webroot
directory, and create a new folder called tasks
. Next, navigate inside the tasks
folder, and create the following folder structure to serve as our application's skeleton:
tasks/ assets/ protected/ commands/ components/ config/ controllers/ data/ migrations/ models/ runtime/ views/ layouts/ projects/ tasks/ site/
Tip
Yii has a built-in tool called yiic
, which can automatically generate a skeleton project. Refer to the quick start guide (http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app) for more details.
Depending upon the web server you are using, you may also need to create a .htaccess
file in the root directory of your tasks
folder. Information about how to set up your application for the web server you are using can be found in the quick start guide (http://www.yiiframework.com/doc/guide/1.1/en/quickstart.apache-nginx-config).
After setting up our skeleton structure, we can first create our configuration file located at protected/config/main.php
. Our configuration file is one of the most important files of our application as it provides Yii with all the critical information necessary to load and configure our application. The configuration file informs Yii about the files to be preloaded by Yii's built-in autoloader, the modules to be loaded, the component to be registered, and any other configuration options we want to pass to our application.
For this application, we will be enabling the Gii module, which will allow us to create models based upon our database structure. We will also enable two components, urlManager
and db
, which will allow us to set up custom routes and access our SQLite database. Have a look at the following code snippet:
<?php return array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'Task Application', 'import'=>array( 'application.models.*', 'application.components.*', ), 'modules'=>array( // Include the Gii Module so that we can //generate models and controllers for our application 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>false, 'ipFilters'=>false ), ), 'components'=>array( 'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'rules'=>array( '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ), // Define where our SQLite database is going to be // stored, relative to our config file 'db'=>array( 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/tasks.db', ) ) );
Next, we can create our index.php
file as follows, which will serve as our bootstrap endpoint for our web application:
<?php // change the following paths if necessary $yii='/opt/frameworks/php/yii/framework/yii.php'; $config=dirname(__FILE__).'/protected/config/main.php'; // remove the following lines when in production mode defined('YII_DEBUG') or define('YII_DEBUG',true); // specify how many levels of call stack should be shown in each log message defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3); require_once($yii); Yii::createWebApplication($config)->run();
Finally, we can create our applications yiic
file in protected/yiic.php
as follows, which will allow us to run console commands native to Yii from our application:
<?php // change the following paths if necessary $config=dirname(__FILE__).'/config/main.php'; $config = require($config); require_once('/opt/frameworks/php/yii/framework/yiic.php');