外貿(mào)網(wǎng)站建設(shè)步驟網(wǎng)店?duì)I銷
yii 常用一些調(diào)用 (增加中)
調(diào)用YII框架中 jquery:Yii::app()->clientScript->registerCoreScript(‘jquery’);
framework/web/js/source的js,其中registerCoreScript key調(diào)用的文件在framework/web/js/packages.php列表中可以查看
在view中得到當(dāng)前controller的ID方法 :Yii::app()->getController()->id;
在view中得到當(dāng)前action的ID方法 :Yii::app()->getController()->getAction()->id;
yii獲取ip地址 :Yii::app()->request->userHostAddress;
yii判斷提交方式 :Yii::app()->request->isPostRequest
得到當(dāng)前域名: Yii::app()->request->hostInfo
得到proteced目錄的物理路徑 :YII::app()->basePath;
獲得上一頁的url以返回 :Yii::app()->request->urlReferrer;
得到當(dāng)前url :Yii::app()->request->url;
得到當(dāng)前home url :Yii::app()->homeUrl
得到當(dāng)前return url :Yii::app()->user->returnUrl
項(xiàng)目路徑 :dirname(Yii::app()->BasePath)
項(xiàng)目目錄 Yii::app()->request->baseUrl 只輸出一個(gè)連接(url)<?php echo $this->createUrl('admin/left_menu');?> //**.php?r=admin/left_menu
輸出一組url(yii url 默認(rèn)樣式)
//除域名外的URL
Yii::app()->request->getUrl();
除域名外的首頁地址
Yii::app()->user->returnUrl;
6、//除域名外的根目錄地址 Yii::app()->homeUrl;
YII FRAMEWORK的COOKIE使用方法
設(shè)置cookie:
[php] view plain copy
$cookie = new CHttpCookie(‘mycookie’,‘this is my cookie’);
c o o k i e ? > e x p i r e = t i m e ( ) + 60 ? 60 ? 24 ? 30 ; / / 有限期 30 天 Y i i : : a p p ( ) ? > r e q u e s t ? > c o o k i e s [ ′ m y c o o k i e ′ ] = cookie->expire = time()+60*60*24*30; //有限期30天 Yii::app()->request->cookies['mycookie']= cookie?>expire=time()+60?60?24?30;//有限期30天Yii::app()?>request?>cookies[′mycookie′]=cookie;
讀取cookie:
[html] view plain copy
$cookie = Yii::app()->request->getCookies();
echo $cookie[‘mycookie’]->value;
銷毀cookie:
[html] view plain copy
$cookie = Yii::app()->request->getCookies();
unset(cookie[name]);
在控制器添加CSS文件或JAVASCRIPT文件
[php] view plain copy
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的用戶驗(yàn)證與授權(quán)
yii提供了CUserIdentity類,這個(gè)類一般用于驗(yàn)證用戶名和密碼的類.繼承后我們需要重寫其中的authenticate()方法來實(shí)現(xiàn)我們自己的驗(yàn)證方法.具體代碼如下:
[php] view plain copy
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
r e c o r d = U s e r : : m o d e l ( ) ? > f i n d B y A t t r i b u t e s ( a r r a y ( ′ u s e r n a m e ′ = > record=User::model()->findByAttributes(array('username'=> record=User::model()?>findByAttributes(array(′username′=>this->username));
if($record===null)
t h i s ? > e r r o r C o d e = s e l f : : E R R O R U S E R N A M E I N V A L I D ; e l s e i f ( this->errorCode=self::ERROR_USERNAME_INVALID; else if( this?>errorCode=self::ERRORU?SERNAMEI?NVALID;elseif(record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
t h i s ? > i d = this->_id= this?>i?d=record->id;
$this->setState(‘title’, $record->title);
KaTeX parse error: Expected 'EOF', got '}' at position 45: …ONE; }? retu…this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
在用戶登陸時(shí)則調(diào)用如下代碼:
// 使用提供的用戶名和密碼登錄用戶
[html] view plain copy
i d e n t i t y = n e w U s e r I d e n t i t y ( u s e r n a m e , p a s s w o r d ) ; i f ( identity=new UserIdentity(username,password); if( identity=newUserIdentity(username,password);if(identity->authenticate())
Yii::app()->user->login($identity);
else
echo $identity->errorMessage;
用戶退出時(shí),則調(diào)用如下代碼:
[html] view plain copy
// 注銷當(dāng)前用戶
Yii::app()->user->logout();
其中的user是yii的一個(gè)components.需要在protected/config/main.php中定義
[html] view plain copy
‘user’=>array(
// enable cookie-based authentication
‘a(chǎn)llowAutoLogin’=>true,
‘loginUrl’ => array(‘site/login’),
),
YII FRAMEWORK中TRASACTION事務(wù)的應(yīng)用
[html] view plain copy
$model=Post::model();
t r a n s a c t i o n = transaction= 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
p o s t = post= post=model->findByPk(10);
$post->title=‘new post title’;
$post->save();
$transaction->commit();
}
catch(Exception $e)
{
$transaction->rollBack();
}
Yii Framework中截取字符串(UTF-8)的方法
Helper.PHP
[php] view plain copy
class Helper extends CController
{
public static function truncate_utf8_string($string, $length, $etc = ‘…’)
{
$result = ‘’;
s t r i n g = h t m l e n t i t y d e c o d e ( t r i m ( s t r i p t a g s ( string = html_entity_decode(trim(strip_tags( string=htmle?ntityd?ecode(trim(stript?ags(string)), ENT_QUOTES, ‘UTF-8’);
s t r l e n = s t r l e n ( strlen = strlen( strlen=strlen(string);
for ( i = 0 ; ( ( i = 0; (( i=0;((i < KaTeX parse error: Expected 'EOF', got '&' at position 9: strlen) &?& (length > 0)); KaTeX parse error: Expected '}', got 'EOF' at end of input: … if (number = strpos(str_pad(decbin(ord(substr($string, KaTeX parse error: Expected '}', got 'EOF' at end of input: … if (length < 1.0)
{
break;
}
r e s u l t . = s u b s t r ( result .= substr( result.=substr(string, $i, $number);
$length -= 1.0;
$i += $number - 1;
}
else
{
r e s u l t . = s u b s t r ( result .= substr( result.=substr(string, $i, 1);
$length -= 0.5;
}
}
r e s u l t = h t m l s p e c i a l c h a r s ( result = htmlspecialchars( result=htmlspecialchars(result, ENT_QUOTES, ‘UTF-8’);
if ($i < $strlen)
{
$result .= $etc;
}
return $result;
}
}
將Helper.php放進(jìn)protected\components文件夾下。
使用方法:
Helper::truncate_utf8_string(content,20,false);//不顯示省略號Helper::truncateutf8string(content,20); //顯示省略號
CBREADCRUMBS簡介~俗稱:面包屑
功能介紹:zii.widgets 下的CBreadcrumbs類,其繼承關(guān)系: CBreadcrumbs ? CWidget ?
CBaseController ? CComponent .源代碼位置:
framework/zii/widgets/CBreadcrumbs.php
面包屑類顯示一個(gè)鏈接列表以表明當(dāng)前頁面在整個(gè)網(wǎng)站中的位置.
由于面包屑通常會出現(xiàn)在網(wǎng)站的近乎所有的頁面,此插件最好在視圖的layout中進(jìn)行部署.
你可以定義一個(gè)breadcrumbs屬性并且在布局文件中指派給(網(wǎng)站)基礎(chǔ)控制器插件,如下所示:
[html] view plain copy
t h i s ? > w i d g e t ( ′ z i i . w i d g e t s . C B r e a d c r u m b s ′ , a r r a y ( ′ l i n k s ′ = > this->widget('zii.widgets.CBreadcrumbs', array( 'links'=> this?>widget(′zii.widgets.CBreadcrumbs′,array(′links′=>this->breadcrumbs,
));
于是乎,你需要時(shí),只需要在每個(gè)視圖腳本中,指定breadcrumbs屬性(就可以顯示出網(wǎng)頁導(dǎo)航了).
以上是官方提供的文檔文件的介紹.
下面介紹視圖文件中寫法:
[html] view plain copy
$this->breadcrumbs=array(
‘Users’=>array(‘index’),
‘Create’,
// 形式 : ‘key’ =>‘value’ key的位置相當(dāng)于最后顯示出來的a標(biāo)簽內(nèi)的名字, value則相當(dāng)于a標(biāo)簽的href屬性.
// 'Create’表示當(dāng)前頁 故沒有設(shè)置鏈接.
);
YII FRAMEWORK中驗(yàn)證碼的使用
1.在controller中修改:
[html] view plain copy
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表單中添加如下代碼:
[html] view plain copy
<?php if(CCaptcha::checkRequirements()): ?>Letters are not case-sensitive.
YII FRAMEWORK的CHTML::LINK支持錨點(diǎn)
CHtml::link(‘鏈接文字’,array(‘a(chǎn)rticle/view’,‘id’=>‘3’,‘#’=>‘錨名稱’);
CUrlManager的 createUrl,是可以支持 ‘#’ 的!
params = array(‘userid’ => 100, ‘#’ => ‘錨名稱’);this->createUrl(route,params);
YII FRAMEWORK在WEB頁面查看SQL語句配置
[html] view plain copy
‘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.*’ //只顯示關(guān)于數(shù)據(jù)庫信息,包括數(shù)據(jù)庫連接,數(shù)據(jù)庫執(zhí)行語句
),
),
),
),
YII FRAMEWORK打印AR結(jié)果
[html] view plain copy
KaTeX parse error: Expected '}', got 'EOF' at end of input: … var_dump(v->attributes);
}
yii 數(shù)據(jù)save后得到插入id
post?>save();//得到上次插入的Insertidid = $post->attributes[‘id’];
如此很簡單
yii獲取ip地址
Yii::app()->request->userHostAddress;
yii execute后獲取insert id
$id = Yii::app()->db->getLastInsertID();
yii獲取get,post過來的數(shù)據(jù)
Yii::app()->request->getParam(‘id’);
yii如何設(shè)置時(shí)區(qū)
可以在config/main.php 里’timeZone’=>‘Asia/Chongqing’,設(shè)定時(shí)區(qū).
yii如何將表單驗(yàn)證提示弄成中文的
將main.php里的app配置加上language=>‘zh_cn’,系統(tǒng)默認(rèn)的提示就是中文的了,要自定義消息就像樓上說的定義message
yii如何獲得上一頁的url以返回
Yii::app()->request->urlReferrer;
yii多對多關(guān)聯(lián)條件
[html] view plain copy
c r i t e r i a ? > a d d I n C o n d i t i o n ( ′ c a t e g o r y s . i d ′ , criteria->addInCondition('categorys.id', criteria?>addInCondition(′categorys.id′,in);
c r i t e r i a ? > a d d S e a r c h C o n d i t i o n ( ′ S h o p . n a m e ′ , k e y w o r d ) ; s h o p s = S h o p : : m o d e l ( ) ? > w i t h ( a r r a y ( ′ c a t e g o r y s ′ = > a r r a y ( ′ t o g e t h e r ′ = > t r u e ) ) ) ? > f i n d A l l ( criteria->addSearchCondition('Shop.name',keyword);shops=Shop::model()->with(array('categorys'=>array('together'=>true)))->findAll( criteria?>addSearchCondition(′Shop.name′,keyword);shops=Shop::model()?>with(array(′categorys′=>array(′together′=>true)))?>findAll(criteria);
同時(shí)要在Shop模型中加入alias=‘categorys’ ,另外together=true放在模型的關(guān)聯(lián)中也可
yii如何防止重復(fù)提交?
提交后Ccontroler->refresh();
yii過濾不良代碼
[html] view plain copy
$purifier=new CHtmlPurifier;
$purifier->options=array(‘HTML.Allowed’=>‘div’);
c o n t e n t = content= content=purifier->purify($content);
或者
[html] view plain copy
…display user-entered content here…
<?php $this->endWidget(); ?>顯示yii的sql語句查詢條數(shù)和時(shí)間
在config/main.php中配置在log組件的routes中加入
[html] view plain copy
array(
‘class’=>‘CProfileLogRoute’,
‘levels’=>‘error, warning’,
)
同時(shí)在db組件中加入’enableProfiling’=>true,同時(shí)在這種情況下,可以用CDbConnection::getStats() 查看執(zhí)行了多少個(gè)語句,用了多少時(shí)間print_r(CDbConnection::getStats());
Yii多數(shù)據(jù)庫操作
大多數(shù)情況下,我們都會采用同一類型的數(shù)據(jù)庫,只是為了緩解壓力分成主從或分布式形式而已。聲明你可以在app config里聲明其它的數(shù)據(jù)庫連接: