yii2 - create (and run) module manually

yii2
voted: 1
vote
php framework yii2 module

Creating module in yii2 can be done in two difrent ways: 1. manually 2. by gii generator Assuming the framework is installed and running and we want to create module called: 'examplemodule'

1. check if folder modules exists in root folder (at the same level as 'models' and 'controllers'). If not create this folder
2. in 'modules' folder create folder named with module name, in this case 'examplemodule'
3. in 'examplemodule' folder create 'controllers' folder
4. in 'controllers' folder create file 'DefaultController.php' with code:
<?php

namespace app\modules\examplemodule\controllers;
use yii\web\Controller;

class DefaultController extends Controller {
    
    //index action
    public function actionIndex() {
        return $this->render('index');
    }
    
    //other action, for example 'menu'
    public function actionMenu() {
        return $this->render('menu');
    }

}

?>

5. in 'controllers' folder create one more controller, for example 'TestController.php' so module will have two different controllers with some actions. Code for 'TestController.php':
<?php

namespace app\modules\examplemodule\controllers;

use yii\web\Controller;

class TestController extends Controller {

    public function actionIndex() {
        return $this->render('index');
    }

    public function actionIndex2() {
        return $this->render('index2');
    }

}
?>

6. in 'examplemodule' folder create 'views' folder
7. in 'views' folder create 'default' folder
8. in 'default' folder create 'index.php' with code:
<?php
    echo 'module examplemodule</br> controller default</br> action index';
?>
9. in 'default' folder create 'menu.php' with code:
<?php
    echo 'module examplemodule</br> controller default</br> action menu';
?>
10. in 'views' folder create 'test' folder
11. in 'test' folder create 'index.php' with code:
<?php
    echo 'module examplemodule</br> controller test</br> action index';
?>
12. in 'test' folder create file 'index2.php' with code:
<?php
    echo 'module examplemodule</br> controller test</br> action index2';
?>
13. in 'examplemodule' folder create file 'Examplemodule.php' with code:
<?php

namespace app\modules\examplemodule;

class Examplemodule extends \yii\base\Module {

    public $controllerNamespace = 'app\modules\examplemodule\controllers';

    public function init() {
        parent::init();

        // custom initialization code goes here
    }

}
?>
14. Enable module in config. In config/web.php file in 'modules' section add entry with 'examplemodule' with code:
<?php
'modules' => [
        //........................
        'examplemodule' => [
            'class' => 'app\modules\examplemodule\Examplemodule',
        ],
        //.........................
    ]
?>
15. after theese steps controllers cal be accessed with urls:
localhost/sitename/web/index.php?r=examplemodule/default/index
localhost/sitename/web/index.php?r=examplemodule/default/menu
localhost/sitename/web/index.php?r=examplemodule/test/index
localhost/sitename/web/index.php?r=examplemodule/test/index2
working url: not entered
version: 2
Leave a Comment:
Characters left:
No comments yet.