让我们创建一个简单的扩展来显示一个标准的“Hello World”消息。这个扩展将通过Packagist库进行分发。
第1步 - 在硬盘驱动器上创建一个名为 yii2-exts-hello-world 的文件夹,但不能在Yii基本的应用程序模板中)。在 yii2-exts-hello-world 目录下,创建一个名为 composer.json 文件,并用下面的代码。
{
"name": "zaixian/hello-world",
"authors": [
{
"name": "zaixian"
}
],
"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 zaixian!';
}
}
?>
我们已经定义了一个SayHello 类和一个 world 的静态函数,用于返回 hello 消息。
第4步 - 扩展已准备就绪。现在创建您的 GitHub 帐户的一个空仓库,并 push 这个扩展。
在 hello-world 文件夹中运行-
- git clone https://github.com/zaixian-com/yii2-exts-hello-world.git
- git add .
- git commit . -m 'add new files'
- git push -u origin master

刚才就发送您的扩展到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": "*",
"zaixian/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消息。
上一篇:
Yii扩展
下一篇:
Yii HTTP请求
