• Gen app
  • Configuring
  • Editing models
  • Basic View Edits
  • Basic Controller Edits

Gen app cd /Users/larryullman/Sites/YiiBlogSite/framework yiic webapp path/to/directory

Configuring

//index.php
$yii=dirname(__FILE__).'/../framework/yii.php';
 
//The second line identifies where the configuration file is:
$config=dirname(__FILE__).'/protected/config/main.php';
/*The default behavior is to put the protected directory, where all the application files reside, in the same directory as the index file. My inclination is to move it outside of the Web directory. Moving the protected folder outside of the Web root directory is just an extra security precaution. It’s not required, and you may not want to bother with the change, especially as you’re just getting started. In such a case, I edit my index.php file to read:*/
$config= '../protected/config/main.php';
 
//The next line of code turns on debugging mode:
defined('YII_DEBUG') or define('YII_DEBUG',true);
 
/*You’ll want debugging enabled when developing a site, but disabled once live. To disable debuggin, remove or comment out that line.*/
//The next line of code dictates how many levels of “call stack” are shown in a message log:
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

Most of the configuration occurs in the main.php configuration file, found within the protected/config directory

* 'name'=>'Wicked Cool Yii Site',
* 'gii'=>array(
* 'urlManager'=>array(
* 'db'=>array(
* 'log'=>array(
* 'params'=>array(
* “sitecontroller default. To change that add:
'defaultController' => 'login',

**Editing models*

public function rules()
{
    return array(
        array('departmentId, firstName, lastName, email, hireDate', 'required'),
        array('departmentId, ext', 'numerical', 'integerOnly'=>true),
        array('firstName', 'length', 'max'=>20),
        array('lastName', 'length', 'max'=>40),
        array('email', 'length', 'max'=>60),
        array('email', 'email'),
        array('leaveDate', 'safe'),
        array('id, departmentId, firstName, lastName, email, ext, hireDate, leaveDate', 'safe', 'on'=>'search'),
 );
}
public function relations()
{
    return array('department' => array(self::BELONGS_TO, 'Department', 'departmentId') );
}
public function relations()
{
    return array('employees' => array(self::HAS_MANY, 'Employee', 'departmentId') );
}
 
//attributeLabels() returns an associative array of fields and the labels to use for those fields in forms, error messages, and so forth. 
public function attributeLabels()
{
    return array(
        'id' => 'Employee ID',
        'departmentId' => 'Department',
        'firstName' => 'First Name',
        'lastName' => 'Last Name',
        'email' => 'Email',
        'ext' => 'Ext',
        'hireDate' => 'Hire Date',
        'leaveDate' => 'Leave Date',
    );
}

**Basic View Edits* - protected/views/layouts/main.php

//To start in the HEAD, you’ll see that external files are linked using
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
 
//Next, you’ll see the page’s title set dynamically:
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
//The CHtml::encode() method is just used to protect against Cross-Site Scripting (XSS) attacks.
 
// in the main layout file:
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
 
/*The create and update Views have some page header stuff, then include the form View, using this code:*/
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>
 
//Finally, you may decide you want to change the page’s title. To do that, use code like:
<?php $this->pageTitle = $model->something; ?>

Basic Controller Edits

//within a Controller class is a variable called $layout:
public $layout='//layouts/column2';
 
//default is actionIndex().
public $defaultAction='admin';

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.