在view中得到當前controller的ID方法:Yii::app()->getController()->id;
在view中得到當前action的ID方法:Yii::app()->getController()->getAction()->id;
輸出一組url(yii url 默認樣式) </div><div><div><?php $this->widget('zii.widgets.CMenu',array(</div><div> 'items'=>array(</div><div> array('label'=>'主菜單', 'url'=>array('/admin/left_menu')),</div><div> array('label'=>'內容發佈', 'url'=>array('/admin/page')),</div><div> array('label'=>'內容維護', 'url'=>array('/site/contact')),</div><div> array('label'=>'系統主頁', 'url'=>array('/site/login')),</div><div> array('label'=>'網站主頁', 'url'=>array('/site/logout')),</div><div> array('label'=>'會員中心', 'url'=>array('/site/login')),</div><div> array('label'=>'註銷', 'url'=>array('/site/login')),</div><div> ),</div><div> )); ?> </div></div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微軟雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">//除域名外的URL</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微軟雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;"> Yii::app()->request->getUrl();</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微軟雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;"> 除域名外的首頁地址</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微軟雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;"> Yii::app()->user->returnUrl;</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; font-size: 13px; font-family: 'Microsoft YaHei', 微軟雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; line-height: 24px;">6、//除域名外的根目錄地址 Yii::app()->homeUrl;</p></div>
//除域名外的URL
Yii::app()->request->getUrl();
除域名外的首頁地址
Yii::app()->user->returnUrl;
6、//除域名外的根目錄地址 Yii::app()->homeUrl;
YII FRAMEWORK的COOKIE使用方法設置cookie:
$cookie = new CHttpCookie('mycookie','this is my cookie');
$cookie->expire = time()+60*60*24*30; //有限期30天
Yii::app()->request->cookies['mycookie']=$cookie;
讀取cookie:$cookie = Yii::app()->request->getCookies();
echo $cookie['mycookie']->value;
銷燬cookie:
$cookie = Yii::app()->request->getCookies();
unset($cookie[$name]);
在控制器添加CSS文件或JAVASCRIPT文件
public function init()
{
parent::init();
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my.css');
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/css/my.js');
}
YII FRAMEWORK的用戶驗證與授權
yii提供了CUserIdentity類,這個類一般用於驗證用戶名和密碼的類.繼承後我們需要重寫其中的authenticate()方法來實現我們自己的驗證方法.具體代碼如下:
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
在用戶登陸時則調用如下代碼:
// 使用提供的用戶名和密碼登錄用戶
$identity=new UserIdentity($username,$password);
if($identity->authenticate())
Yii::app()->user->login($identity);
else
echo $identity->errorMessage;
用戶退出時,則調用如下代碼:
// 註銷當前用戶
Yii::app()->user->logout();
其中的user是yii的一個components.需要在protected/config/main.php中定義
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'loginUrl' => array('site/login'),
),
YII FRAMEWORK中TRASACTION事務的應用
$model=Post::model();
$transaction=$model->dbConnection->beginTransaction();
try
{
// find and save are two steps which may be intervened by another request
// we therefore use a transaction to ensure consistency and integrity
$post=$model->findByPk(10);
$post->title='new post title';
$post->save();
$transaction->commit();
}
catch(Exception $e)
{
$transaction->rollBack();
}
Yii Framework中截取字符串(UTF-8)的方法
Helper.phpclass Helper extends CController
{
public static function truncate_utf8_string($string, $length, $etc = '...')
{
$result = '';
$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
$strlen = strlen($string);
for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
{
if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
{
if ($length < 1.0)
{
break;
}
$result .= substr($string, $i, $number);
$length -= 1.0;
$i += $number - 1;
}
else
{
$result .= substr($string, $i, 1);
$length -= 0.5;
}
}
$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
if ($i < $strlen)
{
$result .= $etc;
}
return $result;
}
}
將Helper.php放進protected\components文件夾下。
CBREADCRUMBS簡介~俗稱:麪包屑
功能介紹:zii.widgets 下的CBreadcrumbs類,其繼承關係: CBreadcrumbs » CWidget »
CBaseController » CComponent .源代碼位置:
framework/zii/widgets/CBreadcrumbs.php
麪包屑類顯示一個鏈接列表以表明當前頁面在整個網站中的位置.
由於麪包屑通常會出現在網站的近乎所有的頁面,此插件最好在視圖的layout中進行部署.
你可以定義一個breadcrumbs屬性並且在佈局文件中指派給(網站)基礎控制器插件,如下所示:
$this->widget('zii.widgets.CBreadcrumbs', array(
'links'=>$this->breadcrumbs,
));
於是乎,你需要時,只需要在每個視圖腳本中,指定breadcrumbs屬性(就可以顯示出網頁導航了).以上是官方提供的文檔文件的介紹.
下面介紹視圖文件中寫法:
$this->breadcrumbs=array(
'Users'=>array('index'),
'Create',
// 形式 : 'key' =>'value' key的位置相當於最後顯示出來的a標籤內的名字, value則相當於a標籤的href屬性.
// 'Create'表示當前頁 故沒有設置鏈接.
);
YII
FRAMEWORK中驗證碼的使用1.在controller中修改:
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF, //背景顏色
'minLength'=>4, //最短爲4位
'maxLength'=>4, //是長爲4位
'transparent'=>true, //顯示爲透明
),
);
}
在view的form表單中添加如下代碼:
<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
YII FRAMEWORK在WEB頁面查看SQL語句配置
'components'=>array(
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
// 下面顯示頁面日誌
array(
'class'=>'CWebLogRoute',
'levels'=>'trace', //級別爲trace
'categories'=>'system.db.*' //只顯示關於數據庫信息,包括數據庫連接,數據庫執行語句
),
),
),
),