Yii框架學(xué)習(xí)筆記之應(yīng)用組件操作示例
本文實(shí)例講述了Yii框架學(xué)習(xí)筆記之應(yīng)用組件操作。分享給大家供大家參考,具體如下:
所有的組件都應(yīng)聲明在config/web.php
//組件聲明在該數(shù)組下 'components'=>array( //自定義組件1 - 函數(shù)形式 'customComponent1' => function(){ $custom = new app\components\CustomComponent\realization\CustomComponent1(); $custom->setName('譚勇'); $custom->setAge(22); return $custom; }, //自定義組件2 - 數(shù)組形式 'customComponent2' => array( 'class' => 'app\components\CustomComponent\relazation\CustomComponent2' 'name' => '譚勇', 'age' => 22 ), //自定義組件 - 字符串形式 'customComponent3' => 'app\components\CustomComponent\realization\CustomComponent3' ),
如果只是在components 中聲明了該組件,那么只有在首次調(diào)用的時(shí)候才會(huì)實(shí)例化這個(gè)組件,之后調(diào)用都會(huì)復(fù)用之前的實(shí)例。 如果你在bootstrap 數(shù)組中聲明了這個(gè)組件,那么該組件會(huì)隨著應(yīng)用主體的創(chuàng)建而實(shí)例(也就是默認(rèn)會(huì)被實(shí)例,而不是首次調(diào)用才會(huì)實(shí)例這個(gè)組件)。
//默認(rèn)加載customComponent1 和 customComponent2 組件 'bootstrap' => array( 'customComponent1','customComponent2' ),
在應(yīng)用目錄下創(chuàng)建 components 目錄
組件 CutomComponent
接口類 app\components\CustomComponent\CustomComponent;
<?php namespace app\components\CustomComponent; interface CustomComponent { public function setName($name); public function setAge($age); public function getName(); public function getAge(); } ?>
接口實(shí)現(xiàn)類 app\components\CustomComponent\realization\CustomComponent1
<?php namespace app\components\CustomComponent\realization; use app\components\CustomComponent\CustomComponent; class CustomComponent1 implments CustomComponent { public $name='勇哥'; public $age = '我的年齡'; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function setAge($age) { $this->age = $age; } public function getAge() { return $this->age; } } ?>
customComponent2,customComponent3 我們都讓他們與customComponent1 具有相同的代碼。 那么我們?cè)趺慈フ{(diào)用這些組件呢?
namespace app\controllers\home; use Yii; use yii\web\Controller; class IndexController extends Controller { public function actionIndex() { //組件customComponent1 echo Yii::$app->customComponent1->getName(); //組件customComponent2 echo Yii::$app->customComponent2->getName(); //組件customComponent3 echo Yii::$app->customComponent3->getName(); } }
然后回過(guò)頭看數(shù)組形式、函數(shù)形式、字符串形式的組件
//函數(shù)形式 - 這個(gè)很容易理解 實(shí)例化后設(shè)置屬性值 function(){ $custom = new app\components\CustomComponent\realization\CustomComponent1(); $custom->setName('譚勇'); $custom->setAge(22); return $custom; }, //數(shù)組形式 - 它會(huì)實(shí)例化這個(gè)組件 之后設(shè)置屬性值 注意這里設(shè)置屬性值的方法 和 函數(shù)不一樣,它是 $custom->name = '譚勇' , $custom->age = 22 array( 'class' => 'app\components\CustomComponent\relazation\CustomComponent2' 'name' => '譚勇', 'age' => 22 ), //字符串形式 只知道會(huì)實(shí)例化這個(gè)組件,怎么注入屬性值,這個(gè)不清楚支不支持
組件有什么作用?
如果你理解Java spring mvc 那么就不難理解組件的作用 可以作為服務(wù)層,數(shù)據(jù)訪問(wèn)層等等
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門(mén)及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門(mén)基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- 從零開(kāi)始學(xué)YII2框架(六)高級(jí)應(yīng)用程序模板
- yii2高級(jí)應(yīng)用之自定義組件實(shí)現(xiàn)全局使用圖片上傳功能的方法
- YII Framework框架使用YIIC快速創(chuàng)建YII應(yīng)用之migrate用法實(shí)例詳解
- YII Framework框架教程之使用YIIC快速創(chuàng)建YII應(yīng)用詳解
- Yii2框架redis基本應(yīng)用示例
- Yii框架常見(jiàn)緩存應(yīng)用實(shí)例小結(jié)
- Yii Framework框架中事件和行為的區(qū)別及應(yīng)用實(shí)例分析
- 再談Yii Framework框架中的事件event原理與應(yīng)用
- Yii框架應(yīng)用組件用法實(shí)例分析
- Yii 框架應(yīng)用(Applications)操作實(shí)例詳解
相關(guān)文章
PHP實(shí)現(xiàn)文件上傳與下載實(shí)例與總結(jié)
這篇文章主要介紹了PHP實(shí)現(xiàn)文件上傳與下載實(shí)例與總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-03-03thinkphp的dump函數(shù)無(wú)輸出實(shí)例代碼
下面小編就為大家?guī)?lái)一篇thinkphp的dump函數(shù)無(wú)輸出實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11基于PHP+Mysql簡(jiǎn)單實(shí)現(xiàn)了圖書(shū)購(gòu)物車系統(tǒng)的實(shí)例詳解
這篇文章主要介紹了基于PHP+Mysql簡(jiǎn)單實(shí)現(xiàn)了圖書(shū)購(gòu)物車系統(tǒng)的實(shí)例詳解,文章通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下 面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08php實(shí)現(xiàn)按文件名搜索文件的遠(yuǎn)程文件查找器
php文件查找程序,輸入一個(gè)路徑確定后會(huì)遍歷目錄下所有的文件和文件夾,通過(guò)遞歸可以找到文件夾下面的每一個(gè)文件,再通過(guò)文件名和輸入的關(guān)鍵字匹配,則可以查找到你想要的文件2014-05-05使用ob系列函數(shù)實(shí)現(xiàn)PHP網(wǎng)站頁(yè)面靜態(tài)化
php頁(yè)面緩存主要用到的是ob系列函數(shù),如ob_start(),ob_end_flush(),ob_get_contents() ,今天我們來(lái)談?wù)勈褂眠@些函數(shù)來(lái)實(shí)現(xiàn)php網(wǎng)站頁(yè)面靜態(tài)化2014-08-08PHP設(shè)計(jì)模式之工廠模式(Factory)入門(mén)與應(yīng)用詳解
這篇文章主要介紹了PHP設(shè)計(jì)模式之工廠模式(Factory),結(jié)合實(shí)例形式詳細(xì)分析了PHP工廠模式的概念、原理、基本應(yīng)用與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-12-12TP框架實(shí)現(xiàn)上傳一張圖片和批量上傳圖片的方法分析
這篇文章主要介紹了TP框架實(shí)現(xiàn)上傳一張圖片和批量上傳圖片的方法,結(jié)合實(shí)例形式分析了TP框架圖片上傳操作相關(guān)原理、實(shí)現(xiàn)步驟及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04