第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/" } } }
第2步 - 建立下面的目錄路徑: yii-exts-hello-world/src/HelloWorld.
<?php namespace HelloWorld; class SayHello { public static function world() { return 'Hello World, Composer from Yiibai!'; } } ?>
剛才就傳送您的擴充套件到GitHub上。 現在,開啟 https://packagist.org,在選單頂部點選「提交」後登入。
{ "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訊息。