《PHP實(shí)戰(zhàn):YiiFramework入門知識(shí)點(diǎn)總結(jié)(圖文教程)》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):YiiFramework入門知識(shí)點(diǎn)總結(jié)(圖文教程),希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
相關(guān)主題:YII框架
本文總結(jié)了YiiFramework入門知識(shí)點(diǎn).分享給大家供大家參考,具體如下:PHP應(yīng)用
創(chuàng)建Yii應(yīng)用骨架PHP應(yīng)用
web為網(wǎng)站根目錄
yiic webapp /web/demoPHP應(yīng)用
通過(guò)GII創(chuàng)建model和CURD時(shí)需要注意PHP應(yīng)用
1、Model Generator 操作PHP應(yīng)用
即使在有表前綴的情況下,Table Name中也要填寫表的全名,即包括表前綴.如下圖:PHP應(yīng)用
PHP應(yīng)用
2、Crud Generator 操作PHP應(yīng)用
該界面中,Model Class中填寫model名稱.首字母大寫.也可參照在生成model時(shí),在proctected/models目錄中通過(guò)model generator生成的文件名.如下圖:PHP應(yīng)用
PHP應(yīng)用
如果對(duì)news、newstype、statustype這三個(gè)表生成CURD控制器,則在Model Generator中,在Model Class中輸入:News、newsType、StatusType.大小寫與創(chuàng)建的文件名的大小寫相同.如果寫成NEWS或NeWs等都不可以.PHP應(yīng)用
創(chuàng)建模塊注意事項(xiàng)PHP應(yīng)用
通過(guò)GII創(chuàng)建模塊,Module ID一般用小寫.無(wú)論如何,這里填寫的ID決定main.php配置文件中的配置.如下:PHP應(yīng)用
'modules'=>array( 'admin'=>array(//這行的admin為Module ID.與創(chuàng)建Module時(shí)填寫的Module ID大寫寫一致 'class'=>'application.modules.admin.AdminModule',//這里的admin在windows os中大小寫無(wú)所謂,但最好與實(shí)際目錄一致. ), ),
路由PHP應(yīng)用
system表示yii框架的framework目錄
application表示創(chuàng)建的應(yīng)用(比如d:\wwwroot\blog)下的protected目錄.
application.modules.Admin.AdminModule
表示應(yīng)用程序目錄(比如:d:\wwwroot\blog\protected)目錄下的modules目錄下的Admin目錄下的AdminModules.php文件(實(shí)際上指向的是該文件的類的名字)
system.db.*
表示YII框架下的framework目錄下的db目錄下的所有文件.PHP應(yīng)用
控制器中的accessRules說(shuō)明PHP應(yīng)用
/** * Specifies the access control rules. * This method is used by the 'accessControl' filter. * @return array access control rules */ public function accessRules() { return array( array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=>array('index','view'),//表示任意用戶可拜訪index、view方法 'users'=>array('*'),//表示任意用戶 ), array('allow', // allow authenticated user to perform 'create' and 'update' actions 'actions'=>array('create','update'),//表示只有認(rèn)證用戶才可操作create、update方法 'users'=>array('@'),//表示認(rèn)證用戶 ), array('allow', // allow admin user to perform 'admin' and 'delete' actions 'actions'=>array('admin','delete'),//表示只有用戶admin才能拜訪admin、delete方法 'users'=>array('admin'),//表示指定用戶,這里指用戶:admin ), array('deny', // deny all users 'users'=>array('*'), ), ); }
看以上代碼注釋.PHP應(yīng)用
user: represents the user session information.詳情查閱API:CWebUser
CWebUser代表一個(gè)Web應(yīng)用程序的持久狀態(tài).
CWebUser作為ID為user的一個(gè)應(yīng)用程序組件.因此,在任何地方都能通過(guò)Yii::app()->user 拜訪用戶狀態(tài)PHP應(yīng)用
public function beforeSave() { if(parent::beforeSave()) { if($this->isNewRecord) { $this->password=md5($this->password); $this->create_user_id=Yii::app()->user->id;//一開(kāi)始這樣寫,User::model()->user->id;(錯(cuò)誤) //$this->user->id;(錯(cuò)誤) $this->create_time=date('Y-m-d H:i:s'); } else { $this->update_user_id=Yii::app()->user->id; $this->update_time=date('Y-m-d H:i:s'); } return true; } else { return false; } }
getter辦法或/和setter辦法PHP應(yīng)用
<?php /** * UserIdentity represents the data needed to identity a user. * It contains the authentication method that checks if the provided * data can identity the user. */ class UserIdentity extends CUserIdentity { /** * Authenticates a user. * The example implementation makes sure if the username and password * are both 'demo'. * In practical applications, this should be changed to authenticate * against some persistent user identity storage (e.g. database). * @return boolean whether authentication succeeds. */ private $_id; public function authenticate() { $username=strtolower($this->username); $user=User::model()->find('LOWER(username)=?',array($username)); if($user===null) { $this->errorCode=self::ERROR_USERNAME_INVALID; } else { //if(!User::model()->validatePassword($this->password)) if(!$user->validatePassword($this->password)) { $this->errorCode=self::ERROR_PASSWORD_INVALID; } else { $this->_id=$user->id; $this->username=$user->username; $this->errorCode=self::ERROR_NONE; } } return $this->errorCode===self::ERROR_NONE; } public function getId() { return $this->_id; } }
model/User.phpPHP應(yīng)用
public function beforeSave() { if(parent::beforeSave()) { if($this->isNewRecord) { $this->password=md5($this->password); $this->create_user_id=Yii::app()->user->id;//====主要為此句.得到登陸帳號(hào)的ID $this->create_time=date('Y-m-d H:i:s'); } else { $this->update_user_id=Yii::app()->user->id; $this->update_time=date('Y-m-d H:i:s'); } return true; } else { return false; } }
更多相關(guān):PHP應(yīng)用
/* 由于CComponent是post最頂級(jí)父類,所以添加getUrl方法....如下說(shuō)明: CComponent 是所有組件類的基類. CComponent 實(shí)現(xiàn)了定義、使用屬性和事件的協(xié)議. 屬性是通過(guò)getter方法或/和setter方法定義.拜訪屬性就像拜訪普通的對(duì)象變量.讀取或?qū)懭雽傩詫⒄{(diào)用應(yīng)相的getter或setter方法 例如: $a=$component->text; // equivalent to $a=$component->getText(); $component->text='abc'; // equivalent to $component->setText('abc'); getter和setter方法的格式如下 // getter, defines a readable property 'text' public function getText() { ... } // setter, defines a writable property 'text' with $value to be set to the property public function setText($value) { ... } */ public function getUrl() { return Yii::app()->createUrl('post/view',array( 'id'=>$this->id, 'title'=>$this->title, )); }
模型中的rules方法PHP應(yīng)用
/* * rules辦法:指定對(duì)模型屬性的驗(yàn)證規(guī)則 * 模型實(shí)例調(diào)用validate或save辦法時(shí)逐一執(zhí)行 * 驗(yàn)證的必須是用戶輸入的屬性.像id,作者id等通過(guò)代碼或數(shù)據(jù)庫(kù)設(shè)定的不用出現(xiàn)在rules中. */ /** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('news_title, news_content', 'required'), array('news_title', 'length', 'max'=>128), array('news_content', 'length', 'max'=>8000), array('author_name, type_id, status_id,create_time, update_time, create_user_id, update_user_id', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, news_title, news_content, author_name, type_id, status_id, create_time, update_time, create_user_id, update_user_id', 'safe', 'on'=>'search'), ); }
說(shuō)明:PHP應(yīng)用
1、驗(yàn)證字段必須為用戶輸入的屬性.不是由用戶輸入的內(nèi)容,無(wú)需驗(yàn)證.
2、數(shù)據(jù)庫(kù)中的操作字段(即使是由系統(tǒng)生成的,比如創(chuàng)建時(shí)間,更新時(shí)間等字段――在boyLee提供的yii_computer源碼中,對(duì)系統(tǒng)生成的這些屬性沒(méi)有放在safe中.見(jiàn)下面代碼).對(duì)于不是表單提供的數(shù)據(jù),只要在rules辦法中沒(méi)有驗(yàn)證的,都要加入到safe中,否則無(wú)法寫入數(shù)據(jù)庫(kù).PHP應(yīng)用
yii_computer的News.php模型關(guān)于rules辦法PHP應(yīng)用
/** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('news_title, news_content', 'required'), array('news_title', 'length', 'max'=>128, 'encoding'=>'utf-8'), array('news_content', 'length', 'max'=>8000, 'encoding'=>'utf-8'), array('author_name', 'length', 'max'=>10, 'encoding'=>'utf-8'), array('status_id, type_id', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, news_title, news_content, author_name, type_id, status_id', 'safe', 'on'=>'search'), ); }
視圖中顯示動(dòng)態(tài)內(nèi)容三種辦法PHP應(yīng)用
1、直接在視圖文件中以PHP代碼實(shí)現(xiàn).比如顯示當(dāng)前時(shí)間,在視圖中:PHP應(yīng)用
控制器辦法中包含:PHP應(yīng)用
$theTime=date("Y-m-d H:i:s"); $this->render('helloWorld',array('time'=>$theTime));
視圖文件:
PHP應(yīng)用
3、視圖與控制器是非常緊密的兄弟,所以視圖文件中的$this指的就是渲染這個(gè)視圖的控制器.修改前面的示例,在控制器中定義一個(gè)類的公共屬性,而不是局部變量,它是值就是當(dāng)前的日期和時(shí)間.然后在視圖中通過(guò)$this拜訪這個(gè)類的屬性.PHP應(yīng)用
視圖命名約定PHP應(yīng)用
視圖文件命名,請(qǐng)與ActionID相同.但請(qǐng)記住,這只是個(gè)推薦的命名約定.其實(shí)視圖文件名不必與ActionID相同,只需要將文件的名字作為第一個(gè)參數(shù)傳遞給render()就可以了.PHP應(yīng)用
DB相關(guān)PHP應(yīng)用
$Prerfp = Prerfp::model()->findAll( array( 'limit'=>'5', 'order'=>'releasetime desc' ) );
$model = Finishrfp::model()->findAll( array( 'select' => 'companyname,title,releasetime', 'order'=>'releasetime desc', 'limit' => 10 ) ); foreach($model as $val){ $noticeArr[] = " 在".$val->title."競(jìng)標(biāo)中,".$val->companyname."中標(biāo)."; }
$model = Cgnotice::model()->findAll ( array( 'select' => 'status,content,updatetime', 'condition'=> 'status = :status ', 'params' => array(':status'=>0), 'order'=>'updatetime desc', 'limit' => 10 ) ); foreach($model as $val){ $noticeArr[] = $val->content; }
$user=User::model()->find('LOWER(username)=?',array($username));
$noticetype = Dictionary::model()->find(array( 'condition' => '`type` = "noticetype"') );
// 查找postID=10 的那一行 $post=Post::model()->find('postID=:postID', array(':postID'=>10));
也可以使用$condition 指定更復(fù)雜的查詢條件.不使用字符串,我們可以讓$condition 成為一個(gè)CDbCriteria 的實(shí)例,它允許我們指定不限于WHERE 的條件.例如:PHP應(yīng)用
$criteria=new CDbCriteria; $criteria->select='title'; // 只選擇'title' 列 $criteria->condition='postID=:postID'; $criteria->params=array(':postID'=>10); $post=Post::model()->find($criteria); // $params 不需要了
注意,當(dāng)使用CDbCriteria 作為查詢條件時(shí),$params 參數(shù)不再需要了,因?yàn)樗梢栽贑DbCriteria 中指定,就像上面那樣.PHP應(yīng)用
一種替代CDbCriteria 的辦法是給find 辦法傳遞一個(gè)數(shù)組.數(shù)組的鍵和值各自對(duì)應(yīng)標(biāo)準(zhǔn)(criterion)的屬性名和值,上面的例子可以重寫為如下:PHP應(yīng)用
$post=Post::model()->find(array( 'select'=>'title', 'condition'=>'postID=:postID', 'params'=>array(':postID'=>10), ));
其它PHP應(yīng)用
1、鏈接
PHP應(yīng)用
具體查找API文檔:CHtml的link()辦法
PHP應(yīng)用
以上兩個(gè)連接效果等同PHP應(yīng)用
組件包含PHP應(yīng)用
一個(gè)示例:PHP應(yīng)用
在視圖中底部有如下代碼:
PHP應(yīng)用
PHP應(yīng)用
打開(kāi)protected/components下的Notice.php文件,內(nèi)容如下:PHP應(yīng)用
<?php Yii::import('zii.widgets.CPortlet'); class Banner extends CPortlet { protected function renderContent() { $this->render('banner'); } }
渲染的視圖banner,是在protected/components/views目錄下.PHP應(yīng)用
具體查看API,關(guān)鍵字:CPortletPHP應(yīng)用
獲取當(dāng)前hostPHP應(yīng)用
Yii::app()->request->getServerName(); //and $_SERVER['HTTP_HOST']; $url = 'http://'.Yii::app()->request->getServerName(); $url .= CController::createUrl('user/activateEmail', array('emailActivationKey'=>$activationKey)); echo $url;
關(guān)于在發(fā)布新聞時(shí)添加ckeditor擴(kuò)展中遇到的情況PHP應(yīng)用
$this->widget('application.extensions.editor.CKkceditor',array( "model"=>$model, # Data-Model "attribute"=>'news_content', # Attribute in the Data-Model "height"=>'300px', "width"=>'80%', "filespath"=>Yii::app()->basePath."/../up/", "filesurl"=>Yii::app()->baseUrl."/up/", );
echo Yii::app()->basePathPHP應(yīng)用
如果項(xiàng)目目錄在:d:\wwwroot\blog目錄下.則上面的值為d:\wwwroot\blog\protected.注意路徑最后沒(méi)有返斜杠.PHP應(yīng)用
echo Yii::app()->baseUrl;PHP應(yīng)用
如果項(xiàng)目目錄在:d:\wwwroot\blog目錄下.則上面的值為/blog.注意路徑最后沒(méi)有返斜杠.PHP應(yīng)用
(d:\wwwroot為網(wǎng)站根目錄),注意上面兩個(gè)區(qū)別.一個(gè)是basePath,一個(gè)是baseUrlPHP應(yīng)用
其它(不一定正確)PHP應(yīng)用
在一個(gè)控制器A對(duì)應(yīng)的A視圖中,調(diào)用B模型中的辦法,采用:B::model()->B模型中的辦法名();PHP應(yīng)用
前期需要掌握的一些API
CHtmlPHP應(yīng)用
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所贊助.PHP應(yīng)用
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP實(shí)戰(zhàn):YiiFramework入門知識(shí)點(diǎn)總結(jié)(圖文教程)》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/7971.html