— — — —Yii的CComponent是Yii的組件基礎類。組件機制,是Yii整個體系的思想精髓,在使用Yii之前,最應該先了解其組件機制,如果不瞭解這個機制,那麼閱讀Yii源代碼會非常喫力。組件機制給Yii框架賦予了無窮的靈活性和可擴展性,可以毫不誇張地說,Yii框架的基礎結構就是組件。大到CApplication對象、控制器、路由管理器(urlManager),小到一些其它插件,均是以組件形式存在的。
—什麼是Yii組件?
Yii中幾乎所有可實例化並繼承自CComponent的類,均可稱爲組件。
—組件的特點是什麼?
繼承自CComponent類(直接繼承或間接繼承),擁有事件及行爲機制,可在配置文件中定義其各個屬性。
—如何創建一個組件?
編寫自定義類,並繼承自CComponent/Component類即可。路徑(\protected\components\***)。
————————CComponent getters和setters 的使用————————————————————————————————————————————————————————————————————
PHP屬性的讀和寫
class MyClass
{
// 隱藏屬性
private $property;
// getter
public function getProperty()
{
return $this->property;
}
// setter
public function setProperty($value)
{
$this->property = $value;
}
}
$object = new MyClass();
// 設置值
$object->setProperty('value');
// 獲取值
echo $object->getProperty();
Yii 組件屬性的讀和寫
class MyClass extends CComponent
{
private $read = 'read only property';
private $write = 'write only property';
public function getRead()
{
return $this->read;
}
public function setWrite($value)
{
$this->write = $value;
}
}
$object = new MyClass();
// 這裏會報錯,因爲我們正試圖寫入只讀屬性。
$object->read = 'value';
// 顯示 'read only property'
echo $object->read;
// 這裏會報錯,因爲我們正試圖讀取只寫屬性。
echo $object->write;
// 將 'value' 寫入私有變量 $write中去
$object->write = 'value';
爲了允許像上面那種 屬性風格 使用 getters 和 setters ,CComponent 使用PHP 魔術方法:__get,__set,__isset,和__unset
Yii深入地使用了這項技術。例如:當你調用Yii::app()->user->id 來獲取當前登錄用戶id,實際上調用的是Yii::app()->getUser()->getId()。
__________________CComponent _事件的使用___________________________________________________________________________________________________
1. 定義事件名稱,其實就是級組件定義一個on開頭的方法,其中的代碼是固定的,
public function onBeginRequest($event){
//public void raiseEvent(string $name, CEvent $event)
$this->raiseEvent('onBeginRequest',$event);
}
即函數名與事件名是一致的。此步的作用就是將綁定在此事件上的處理函數逐個執行。
2. 給組件對象綁定事件處理函數
$component->attachEventHandler($name, $handler);
$component->onBeginRequest = $handler ;
yii支持一個事件綁定多個回調函數,上述的兩個方法都會在已有的事件上增加新的回調函數,而不會覆蓋已有回調函數。$handler即是一個PHP回調函數。
3. 在需要觸發事件的時候,直接激活組件的事件,即調用事件即可
________________________實例1_____________________________________________________
我們有一個博客應用程序,我們需要在有新評論(Comment)提交到博客(Post)的時候就發一份郵件。
Comment是個用Gii生成的標準AR模型。Post也同樣是Gii生成的模型,除了一些自定義的方法。我們需要定義一個事件NewCommentEvent來存儲Post和Comment 兩個模型和句柄類Notifier,它將處理工作。
1.讓我們開始創建protected/components/NewCommentEvent.php:
class NewCommentEvent extends CModelEvent {
public $comment;
public $post;
}
2.現在我們去看看protected/models/Post.php。爲了突出顯示添加了什麼,我們省略了所有標準的AR方法。
class Post extends CActiveRecord {
// 給添加評論到當前提交上定義方法
function addComment(Comment $comment){
$comment->post_id = $this->id;
// 創建事件類實例
$event = new NewCommentEvent($this);
$event->post = $this;
$event->comment = $comment;
// 觸發事件
$this->onNewComment($event);
return $event->isValid;
}
// 定義 onNewComment 事件
public function onNewComment($event) {
// 事實上事件是在這裏觸發的.這樣的話我們可以使用
// onNewComment 方法來取代 raiseEvent.
$this->raiseEvent('onNewComment', $event);
}
}
3.現在,實現Notifie。創建 protected/components/Notifier.php輸入如下代碼:
class Notifier {
function comment($event){
$text = "There was new comment from {$event->comment->author}
on post {$event->post->title}";
mail('[email protected]', 'New comment', $text);
}
}
4.現在是時候在protected/controllers/PostController.php中一起進行操作了。
class PostController extends CController
{
unction actionAddComment()
{
$post = Post::model()->findByPk(10);
$notifier = new Notifier();
// 附加事件句柄
$post->onNewComment = array($notifier, 'comment');
// 在真實的應用中,數據應來自$_POST
$comment = new Comment();
$comment->author = 'Sam Dark';
$comment->text = 'Yii events are amazing!';
// 添加評論
$post->addComment($comment);
}
}
5.所有的評論都將被添加後,管理員將會收到一份相關的郵件。