• 91 9823089875

How to create Yii2 extension - Hello World Example

1) Create the extension in a separate yii2 environment

Create controller HelloWorld.php      

namespace arbizsolutions\sampleextension;
class HelloWorld {
function hw() {
return "HelloWorld";
}
}

2) Create composer.json

{
"name": "arbizsolutions/
sampleextension ",
"description": "This function displays Hello World",
"type": "yii2-extension",
"keywords": ["helloworld","yii2","extension"],
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/arsolutions/yii2-helloworld/issues",
"source": "https://github.com/arsolutions/yii2-helloworld"
},
"minimum-stability": "dev",
"require": {
"yiisoft/yii2": "*"
},
"autoload": {
"psr-4": {
"
arbizsolutions \\sampleextension\\": ""
         }
    }
}

3) Create an account on GitHub. Push extension code on github

4) Create an account on packagist

5) Submit Package

6) In composer.js of Yii2 extensions add the following under require section:

"arbizsolutions/sampleextension":"dev-master"

7) Use update composer.Note : Add --prefer -dist to get the distribution instead of source and to avoid .git folder

8) Under config/main.php in components section ,

'helloWorld' => [
'class' => 'arbizsolutions\sampleextension\HelloWorld'
],
}

9) In your controller use

$var = Yii::$app->helloWorld;
echo $var->hw();


Share