English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Creating Extensions
  • 时间:2024-11-03

Yii - Creating Extensions


Previous Page Next Page  

Let us create a simple extension displaying a standard “Hello world” message. This extension will be distributed via the Packagist repository.

Step 1 − Create a folder called hello-world in your hard drive but not inside the Yii basic apppcation template). Inside the hello-world directory, create a file named composer.json with the following code.

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

We have declared that we are using the PSR-0 standard and all extension files are under the src folder.

Step 2 − Create the following directory path: hello-world/src/HelloWorld.

Step 3 − Inside the HelloWorld folder, create a file called SayHello.php with the following code.

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

We have defined a SayHello class with a world static function, which returns our hello message.

Step 4 − The extension is ready. Now create an empty repository at your github account and push this extension there.

Inside the hello-world folder run −

    git init

    git add

    git commit -m “initial commit”

    git remote add origin <YOUR_NEWLY_CREATED_REPOSITORY>

    git push -u origin master

Push Extensions

We have just sent our extension to the github. Now, go to the https://packagist.org, sign in and cpck “submit” at the top menu.

You will see a page where you should enter your github repository to pubpsh it.

Enter github Repository

Step 5 − Cpck the “check” button and your extension is pubpshed.

Extension Pubpsh

Step 6 − Go back to the basic apppcation template. Add the extension to the 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",
   "pcense": "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-stabipty": "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"
            }
         ],
         "generateCookieVapdationKey": [
            "config/web.php"
         ]
      },
      "asset-installer-paths": {
         "npm-asset-pbrary": "vendor/npm",
         "bower-asset-pbrary": "vendor/bower"
      }
   }
}

Step 7 − Inside the project root folder, run the composer update to install/update all the dependencies.

Run Composer Update

Step 8 − Our extension should be installed. To use it, modify the About view of the actionAbout method of the SiteController.

<?php
   /* @var $this yiiwebView */
   use yiihelpersHtml;
   $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 );
?>
<span 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><?= HelloWorldSayHello::world();  ?></h1>
</span>

Step 9 − Type http://localhost:8080/index.php?r=site/about in the web browser. You will see a hello world message from our extension.

Hello World Message Advertisements