framework/web/js/source的js,其中registerCoreScript key調用的文件在framework/web/js/packages.php列表中可以查看
在view中得到當前controller的ID方法 :Yii::app()->getController()->id;
在view中得到當前action的ID方法 :Yii::app()->getController()->getAction()->id;
yii獲取ip地址 :Yii::app()->request->userHostAddress;
yii判斷提交方式 :Yii::app()->request->isPostRequest
得到當前域名: Yii::app()->request->hostInfo
得到proteced目錄的物理路徑 :YII::app()->basePath;
獲得上一頁的url以返回 :Yii::app()->request->urlReferrer;
得到當前url :Yii::app()->request->url;
得到當前home url :Yii::app()->homeUrl
得到當前return url :Yii::app()->user->returnUrl
項目路徑 :dirname(Yii::app()->BasePath)
項目目錄 Yii::app()->request->baseUrl
只輸出一個連接(url)<?php echo $this->createUrl('admin/left_menu');?> //**.php?r=admin/left_menu
輸出一組url(yii url 默認樣式)
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'主菜單', 'url'=>array('/admin/left_menu')),
array('label'=>'內容發佈', 'url'=>array('/admin/page')),
array('label'=>'內容維護', 'url'=>array('/site/contact')),
array('label'=>'系統主頁', 'url'=>array('/site/login')),
array('label'=>'網站主頁', 'url'=>array('/site/logout')),
array('label'=>'會員中心', 'url'=>array('/site/login')),
array('label'=>'註銷', 'url'=>array('/site/login')),
),
)); ?>
//除域名外的URL
Yii::app()->request->getUrl();
除域名外的首頁地址
Yii::app()->user->returnUrl;
6、//除域名外的根目錄地址
Yii::app()->homeUrl;
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中截取字符串(UTF-8)的方法
Helper.php
class 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文件夾下。
使用方法:
Helper::truncate_utf8_string($content,20,false); //不顯示省略號
Helper::truncate_utf8_string($content,20); //顯示省略號
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, //顯示爲透明
),
);
}
2.在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的CHTML::LINK支持錨點
CHtml::link('鏈接文字',array('article/view','id'=>'3','#'=>'錨名稱');
CUrlManager的 createUrl,是可以支持 '#' 的!
$params = array('userid' => 100, '#' => '錨名稱');
$this->createUrl($route, $params);
'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.*' //只顯示關於數據庫信息,包括數據庫連接,數據庫執行語句
),
),
),
),
YII FRAMEWORK打印AR結果
$user = 模型->model()->findAll();
foreach($user $v) {
var_dump($v->attributes);
}
yii 數據save後得到插入id
$post->save();
//得到上次插入的Insert id
$id = $post->attributes['id'];
yii獲取ip地址
Yii::app()->request->userHostAddress;
yii execute後獲取insert id
$id = Yii::app()->db->getLastInsertID();
yii獲取get,post過來的數據
Yii::app()->request->getParam('id');
yii如何設置時區
可以在config/main.php 裏'timeZone'=>'Asia/Chongqing',設定時區.yii如何將表單驗證提示弄成中文的
將main.php裏的app配置加上language=>'zh_cn',系統默認的提示就是中文的了,要自定義消息就像樓上說的定義messageyii如何獲得上一頁的url以返回
Yii::app()->request->urlReferrer;
yii多對多關聯條件
$criteria->addInCondition('categorys.id',$in);
$criteria->addSearchCondition('Shop.name',$keyword);$shops=Shop::model()->with(array('categorys'=>array('together'=>true)))->findAll($criteria);
同時要在Shop模型中加入alias='categorys' ,另外together=true放在模型的關聯中也可
yii如何防止重複提交?
yii過濾不良代碼
$purifier=new CHtmlPurifier;
$purifier->options=array('HTML.Allowed'=>'div');
$content=$purifier->purify($content);
或者<?php $this->beginWidget('CHtmlPurifier'); ?>
...display user-entered content here...
<?php $this->endWidget(); ?>
顯示yii的sql語句查詢條數和時間
array(
'class'=>'CProfileLogRoute',
'levels'=>'error, warning',
)
同時在db組件中加入'enableProfiling'=>true,同時在這種情況下,可以用CDbConnection::getStats() 查看執行了多少個語句,用了多少時間print_r(CDbConnection::getStats());
Yii多數據庫操作
大多數情況下,我們都會採用同一類型的數據庫,只是爲了緩解壓力分成主從或分佈式形式而已。聲明你可以在app config裏聲明其它的數據庫連接:<?php
......
'components'=>array(
'db'=>....// 主鏈接
'db1'=>...// 從連接1
'db2'=>...// 從連接2
)
......操作在代碼裏,可以通過Yii::app()->db1和Yii::app()->db2獲得兩個從連接。高級操作更高級(自動)的主從數據庫功能將在1.1實現。