Yii建立擴充套件


讓我們建立一個簡單的擴充套件來顯示一個標準的「Hello World」訊息。這個擴充套件將通過Packagist庫進行分發。

第1步 - 在硬碟驅動器上建立一個名為 yii2-exts-hello-world 的檔案夾,但不能在Yii基本的應用程式模板中)。在 yii2-exts-hello-world 目錄下,建立一個名為 composer.json 檔案,並用下面的程式碼。

{
    "name": "yiibai/hello-world",
    "authors": [
        {
            "name": "yiibai"
        }
    ],
    "require": {},
    "autoload": {
        "psr-0": {
            "HelloWorld": "src/"
        }
    }
}
我們已經宣告要使用PSR-0標準,所有的擴充套件名的檔案在 src 檔案夾下。

第2步 - 建立下面的目錄路徑: yii-exts-hello-world/src/HelloWorld.

第3步 - 在 HelloWorld 檔案夾中,建立一個名為SayHello.php 的檔案,並用下面的程式碼。
<?php
   namespace HelloWorld;
   class SayHello {
      public static function world() {
         return 'Hello World, Composer from Yiibai!';
      }
   }
?>
我們已經定義了一個SayHello 類和一個 world 的靜態函式,用於返回 hello 訊息。
第4步 - 擴充套件已準備就緒。現在建立您的 GitHub 帳戶的一個空倉庫,並 push 這個擴充套件。
在 hello-world 檔案夾中執行-

Yii創建擴展
剛才就傳送您的擴充套件到GitHub上。 現在,開啟 https://packagist.org,在選單頂部點選「提交」後登入。

你會看到一個頁面,現在進入你的 GitHub 儲存庫發布。
第5步 - 點選「check」按鈕,您的擴充套件就被公布了。

第6步 - 回到基本應用程式模板。新增此擴充套件到 composer.json。
{
   "name": "yiisoft/yii2-app-basic",
   "description": "Yii 2 Basic Project Template",
   "keywords": ["yii2", "framework", "basic", "project template"],
   "homepage": "http://www.yiiframework.com/",
   "type": "project",
   "license": "BSD-3-Clause",
   "support": {
      "issues": "https://github.com/yiisoft/yii2/issues?state=open",
      "forum": "http://www.yiiframework.com/forum/",
      "wiki": "http://www.yiiframework.com/wiki/",
      "irc": "irc://irc.freenode.net/yii",
      "source": "https://github.com/yiisoft/yii2"
   },
   "minimum-stability": "dev",
   "prefer-stable" : true,
   "require": {
      "php": ">=5.4.0",
      "yiisoft/yii2": ">=2.0.5",
      "yiisoft/yii2-bootstrap": "*",
      "yiisoft/yii2-swiftmailer": "*",
      "kartik-v/yii2-widget-datetimepicker": "*",
      "yiibai/hello-world": "*"
   },
   "require-dev": {
      "yiisoft/yii2-codeception": "*",
      "yiisoft/yii2-debug": "*",
      "yiisoft/yii2-gii": "*",
      "yiisoft/yii2-faker": "*"
   },
   "config": {
      "process-timeout": 1800
   },
   "scripts": {
      "post-create-project-cmd": [
         "yii\\composer\\Installer::postCreateProject"
      ]
   },
   "extra": {
      "yii\\composer\\Installer::postCreateProject": {
         "setPermission": [
            {
               "runtime": "0777",
               "web/assets": "0777",
               "yii": "0755"
            }
         ],
         "generateCookieValidationKey": [
            "config/web.php"
         ]
      },
      "asset-installer-paths": {
         "npm-asset-library": "vendor/npm",
         "bower-asset-library": "vendor/bower"
      }
   }
} 

第7步 - 在專案的根檔案夾中,執行 composer update 來安裝/更新所有的依賴關係。

第8步 - 輸出擴充套件應安裝。要使用它,修改 SiteController 的 actionAbout 方法的About 檢視。

<?php
   /* @var $this yii\web\View */
   use yii\helpers\Html;
   $this->title = 'About';
   $this->params['breadcrumbs'][] = $this->title;
   $this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, developing, views,
      meta, tags']);
   $this->registerMetaTag(['name' => 'description', 'content' => 'This is the
      description of this page!'], 'description');
?>
<div class = "site-about">
   <h1><?= Html::encode($this->title) ?></h1>
   <p>
      This is the About page. You may modify the following file to customize its content:
   </p>
   <h1><?= HelloWorld\SayHello::world();  ?></h1>
</div>

第9步 ? 在瀏覽器中輸入URL=> http://localhost:8080/index.php?r=site/about 並存取,會看到從我們的擴充套件中顯示的一個Hello World訊息。

<