Yii框架官方指南系列35—擴展Yii:創建擴展

黄舟
發布: 2023-03-05 18:30:01
原創
1110 人瀏覽過



由於擴充意味著是第三方開發者使用,需要一些額外的努力去創建它。以下是一些一般性的指導原則:

*擴展最好是自己自給自足。也就是說,其外部的依賴應是最少的。如果使用者的擴充功能需要安裝額外的軟體包,類別或資源檔案,這將是一個頭痛的問題。 *檔案屬於同一個擴充的,應組織在同一目錄下,目錄名稱用擴充名稱。 *擴展裡面的類別應使用一些單字字母前綴,以避免與其他擴展命名衝突。 *擴充功能應該提供詳細的安裝和API文件。這將減少其他開發員使用擴充時所花費的時間和精力。 *擴展應該用適當的許可。如果您認為您的擴充功能在開源和閉源專案中使用,您可以考慮使用許可證,如BSD的,麻省理工學院等,但不是GPL的,因為它要求其衍生的程式碼是開源的。

在下面,我們根據 overview中所描述的分類,描述如何建立一個新的擴充。當您要建立一個主要用於在您自己專案的component零件,這些描述也適用。

1. Application Component(應用程式元件)

一個application component 應實作介面IApplicationComponent或繼承CApplicationComponent。主要需要實作的方法是 IApplicationComponent::init,元件在此執行一些初始化工作。此方法在部件建立和屬性值(在application configuration裡指定的 )被賦值後呼叫。

預設情況下,一個應用程式部件創建和初始化,只有當它首次訪問期間要求處理。如果一個應用程式元件需要在應用程式實例被創建後創建,它應要求使用者在CApplication::preload 的屬性中列出他的編號。

2. Behavior

To create a behavior, one must implement the IBehavior interface. For convenience, Yii provides a base class CBehavior that already implements methodface and provides someface they intend to make。 cord. For example, the CActiveRecordBehavior class implements a set of methods to respond to the life cycle events raised in an ActiveRecord object. A child class can thus override these methods to put in customized codei will 膜 pate 區example of an ActiveRecord behavior. When this behavior is attached to an AR object and when the AR object is being saved by calling

save()

, it will auto. timestamp.

class TimestampBehavior extends CActiveRecordBehavior
{
    public function beforeSave($event)
    {
        if($this->owner->isNewRecord)
            $this->owner->create_time=time();
        else
            $this->owner->update_time=time();
    }
}
登入後複製
3. Widget(小工具)


widget應繼承CWidget或其子類別。 A widget should extend from CWidget or its child classes.

最簡單的方式建立一個新的小工具是繼承一個現成的小工具和重載它的方法或改變其預設的屬性值。例如,如果您想要為CTabView使用更好的CSS樣式,您可以配置其CTabView::cssFile屬性,當使用的小工具時。您也可以繼承CTabView如下,讓您在使用小工具時,不再需要配置屬性。

class MyTabView extends CTabView
{
    public function init()
    {
        if($this->cssFile===null)
        {
            $file=dirname(__FILE__).DIRECTORY_SEPARATOR.'tabview.css';
            $this->cssFile=Yii::app()->getAssetManager()->publish($file);
        }
        parent::init();
    }
}
登入後複製

在上面的,我們重載CWidget::init方法和指定CTabView::cssFile的URL到我們的新的預設CSS樣式如果此屬性未設定時。我們把新的CSS樣式檔案和

MyTabView
類檔案放在相同的目錄下,以便他們能夠封裝成擴充。由於CSS樣式文件不是透過Web訪問,我們需要發布作為asset資源。

要從零開始建立一個新的小工具,我們主要是需要實作兩個方法:CWidget::init 和CWidget::run。第一種方法是當我們在視圖中使用

$this->beginWidget

插入一個小工具時被調用,第二種方法在

$this->endWidget

被調用時調用。如果我們想要在這兩個方法呼叫之間捕捉和處理顯示的內容,我們可以開始output buffering在CWidget::init 和在CWidget::run中回收緩衝輸出作進一步處理。 If we want to capture and process the content displayed between these two method invocations, we can start output buffering in CWidget::init and retrieve the buffered output in CWidget::run for further processing.<p>在网页中使用的小工具,小工具往往包括CSS,Javascript或其他资源文件。我们叫这些文件<em>assets</em>,因为他们和小工具类在一起,而且通常Web用户无法访问。为了使这些档案通过Web访问,我们需要用CWebApplication::assetManager发布他们,例如上述代码段所示。此外,如果我们想包括CSS或JavaScript文件在当前的网页,我们需要使用CClientScript注册 :</p> <p><br></p> <p></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">class MyWidget extends CWidget {     protected function registerClientScript()     {         // ...publish CSS or JavaScript file here...         $cs=Yii::app()-&gt;clientScript;         $cs-&gt;registerCssFile($cssFile);         $cs-&gt;registerScriptFile($jsFile);     } }</pre><div class="contentsignin">登入後複製</div></div> <p></p> <p>小工具也可能有自己的视图文件。如果是这样,创建一个目录命名<code>views在包括小工具类文件的目录下,并把所有的视图文件放里面。在小工具类中使用$this->render('ViewName') 来render渲染小工具视图,类似于我们在控制器里做。

4. Action(动作)

action应继承CAction或者其子类。action要实现的主要方法是IAction::run 。

5. Filter(过滤器)

filter应继承CFilter 或者其子类。filter要实现的主要方法是CFilter::preFilter和CFilter::postFilter。前者是在action之前被执行,而后者是在之后。


class MyFilter extends CFilter
{
    protected function preFilter($filterChain)
    {
        // logic being applied before the action is executed
        return true; // false if the action should not be executed
    }

    protected function postFilter($filterChain)
    {
        // logic being applied after the action is executed
    }
}
登入後複製

参数$filterChain的类型是CFilterChain,其包含当前被filter的action的相关信息。

6. Controller(控制器)

controller要作为扩展需继承CExtController,而不是 CController。主要的原因是因为CController 认定控制器视图文件位于application.views.ControllerID 下,而CExtController认定视图文件在views目录下,也是包含控制器类目录的一个子目录。因此,很容易重新分配控制器,因为它的视图文件和控制类是在一起的。

7. Validator(验证)

Validator需继承CValidator和实现CValidator::validateAttribute方法。


class MyValidator extends CValidator
{
    protected function validateAttribute($model,$attribute)
    {
        $value=$model->$attribute;
        if($value has error)
            $model->addError($attribute,$errorMessage);
    }
}
登入後複製

8. Console Command(控制台命令)

console command 应继承CConsoleCommand和实现CConsoleCommand::run方法。 或者,我们可以重载CConsoleCommand::getHelp来提供 一些更好的有关帮助命令。


class MyCommand extends CConsoleCommand
{
    public function run($args)
    {
        // $args gives an array of the command-line arguments for this command
    }

    public function getHelp()
    {
        return 'Usage: how to use this command';
    }
}
登入後複製

9. Module(模块)

请参阅modules一节中关于就如何创建一个模块。

一般准则制订一个模块,它应该是独立的。模块所使用的资源文件(如CSS , JavaScript ,图片),应该和模块一起分发。还有模块应发布它们,以便可以Web访问它们 。

10. Generic Component(通用组件)

开发一个通用组件扩展类似写一个类。还有,该组件还应该自足,以便它可以很容易地被其他开发者使用。

以上就是Yii框架官方指南系列35——扩展Yii:创建扩展的内容,更多相关内容请关注PHP中文网(www.php.cn)!


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!