Yii - 建立擴充套件



讓我們建立一個簡單的擴充套件,顯示標準的“Hello world”訊息。此擴充套件將透過 Packagist 倉庫分發。

步驟 1 - 在您的硬碟驅動器上建立一個名為hello-world的資料夾(但不要放在 Yii 基本應用程式模板內)。在 hello-world 目錄中,建立一個名為composer.json的檔案,其中包含以下程式碼。

{
    "name": "tutorialspoint/hello-world",
    "authors": [
        {
            "name": "tutorialspoint"
        }
    ],
    "require": {},
    "autoload": {
        "psr-0": {
            "HelloWorld": "src/"
        }
    }
}

我們聲明瞭我們使用 PSR-0 標準,所有擴充套件檔案都在src資料夾下。

步驟 2 - 建立以下目錄路徑:hello-world/src/HelloWorld

步驟 3 - 在HelloWorld資料夾中,建立一個名為SayHello.php的檔案,其中包含以下程式碼。

<?php
   namespace HelloWorld;
   class SayHello {
      public static function world() {
         return 'Hello World, Composer!';
      }
   }
?>

我們定義了一個SayHello類,它包含一個world靜態函式,該函式返回我們的hello訊息。

步驟 4 - 擴充套件已準備就緒。現在在您的github帳戶中建立一個空倉庫並將此擴充套件推送到那裡。

hello-world資料夾內執行 -

  • git init
  • git add .
  • git commit -m “initial commit”
  • git remote add origin <YOUR_NEWLY_CREATED_REPOSITORY>
  • git push -u origin master
Push Extensions

我們剛剛將我們的擴充套件傳送到github。現在,轉到https://packagist.org,登入並點選頂部選單中的“提交”

您將看到一個頁面,您應該在其中輸入您的 github 倉庫以釋出它。

Enter github Repository

步驟 5 - 點選“檢查”按鈕,您的擴充套件程式將釋出。

Extension Publish

步驟 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": "*",
      "tutorialspoint/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以安裝/更新所有依賴項。

Run Composer Update

步驟 8 - 我們的擴充套件應該已安裝。要使用它,請修改SiteControlleractionAbout方法的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 - 在 Web 瀏覽器中鍵入https://:8080/index.php?r=site/about。您將看到來自我們擴充套件的hello world訊息。

Hello World Message
廣告