Yii-extensions

提供:Dev Guides
移動先:案内検索

Yii-拡張機能

拡張機能は、Yiiアプリケーションで使用するために特別に設計されたパッケージです。 独自のコードを拡張機能として共有したり、サードパーティの拡張機能を使用してアプリケーションに機能を追加したりできます。

拡張機能を使用する

ほとんどの拡張機能はComposerパッケージとして配布されます。 Composerは、ComposerパッケージのリポジトリであるPackagistからパッケージをインストールします。

サードパーティの拡張機能をインストールするには、次のようにする必要があります-

  • 拡張機能を composer.json ファイルに追加します。
  • composer installを実行します。

日付と時刻ウィジェットの追加

プロジェクトにきちんとした datetime ウィジェットを追加しましょう。

ステップ1 *-この方法で、基本アプリケーションテンプレートの *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": "stable",
   "require": {
      "php": ">=5.4.0",
      "yiisoft/yii2": ">=2.0.5",
      "yiisoft/yii2-bootstrap": "*",
      "yiisoft/yii2-swiftmailer": "*",
      "kartik-v/yii2-widget-datetimepicker": "*"
   },
   "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"
      }
   }
}

必要なセクションに依存関係* "kartik-v/yii2-widget-datetimepicker": "*" *を追加しました。

  • ステップ2 *-次に、プロジェクトルート内でcomposer updateを実行し、すべての依存関係を更新します。

依存関係の追加

拡張機能をインストールしました。 vendor/kartik-v/yii2widget-datetimepicker フォルダー内にあります。

ステップ3 *-ページに新しくインストールされたウィジェットを表示するには、 *SiteControlleractionAbout メソッドの About ビューを変更します。

<?php
  /*@var $this yii\web\View*/
   use kartik\datetime\DateTimePicker;
   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>
   <?php
      echo DateTimePicker::widget([
         'name' => 'dp_1',
         'type' => DateTimePicker::TYPE_INPUT,
         'value' => '23-Feb-1982 10:10',
         'pluginOptions' => [
            'autoclose'=>true,
            'format' => 'dd-M-yyyy hh:ii'
         ]
      ]);
   ?>
</div>

ステップ4 *-次に、 *php -S localhost:8080t web コマンドを使用して、プロジェクトルートから組み込みのPHPサーバーを実行します。

ステップ5 *- http://localhost:8080/index.php?r = site/about。に移動します。次のスクリーンショットに示すように、きちんとした *datetime ピッカーが表示されます。

日付時刻ピッカー