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类,该类返回了您好消息。

步骤4-扩展已准备就绪。现在在您的github帐户处创建一个空的存储库,并将此扩展名推送到那里。

hello-world文件夹中运行-

  • git初始化
  • git添加
  • git commit -m“初始提交”
  • git remote添加来源<YOUR_NEWLY_CREATED_REPOSITORY>
  • git push -u原始主机
Yii-创建扩展

我们刚刚将扩展名发送到github。现在,转到https://packagist.org,登录并单击顶部菜单中的“提交”

您将看到一个页面,您应在其中输入github存储库以进行发布。

Yii-创建扩展

步骤5-单击“检查”按钮,您的扩展程序即被发布。

Yii-创建扩展

步骤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来安装/更新所有依赖项。

Yii-创建扩展

步骤8-应该安装我们的扩展程序。要使用它,修改关于视图的actionAbout的方法SiteController

<?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步-类型的http://本地主机:8080 / index.php的R =网站/有关?在Web浏览器。您会从我们的扩展程序中看到一个世界问候消息。

Yii-创建扩展

作者:terry,如若转载,请注明出处:https://www.web176.com/yii/540.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年10月26日 下午4:32
下一篇 2020年10月26日 下午4:37

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注