Dominus77 / yii2-advanced-start

Yii2 Start Project Advanced Template

Home Page:https://dominus77.github.io/yii2-advanced-start/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to add new modules

agussudarmanto opened this issue · comments

Hi Sir,
I wants to add new module, is there any tutorial for that, if any i appreciate step-by-step tutorial, because i was use gii inside backend to generate modules and add in config/main.php, but it can't run and show 404.

Thanks for your answer sir.

Hello!
Generating a module through gii in this template is no different from the standard, it is important to specify the correct data.
https://www.yiiframework.com/doc/guide/2.0/en/start-gii

  1. Module Generator
    Module Class: modules\example\Module
    Module ID: example
    Click Preview and Generate

The module will be generated in the modules directory of the root directory: https://github.com/Dominus77/yii2-advanced-start/tree/master/modules

  1. Next, you should edit the newly generated Module.php file. An example can be looked at in any of the ready-made modules, for example, in the module main: https://github.com/Dominus77/yii2-advanced-start/blob/master/modules/main/Module.php.
    It is worth paying attention to where you have controllers, their namespace:
    public $controllerNamespace = 'modules\main\controllers\frontend';

    $this->controllerNamespace = 'modules\main\controllers\backend';

    And files views:
    $this->setViewPath('@modules/main/views/backend');

    $this->setViewPath('@modules/main/views/frontend');
  2. You also need to copy from the finished module to the new one, the Bootstrap.php file. Edit it according to the requirements of the new module. This file contains the i18n connection and the urlManager rules for the module.

Usage:
backend/config/main.php

'modules\main\Bootstrap',

'main' => [
'isBackend' => true,
],

common/config/main.php
'main' => [
'class' => 'modules\main\Module',
],

frontend/config/main.php
'modules\main\Bootstrap',

That's it, the module will be available http://mysite.com/example/default/index
and http://mysite.com/admin/example/default/index

URL depends on the specified rules in the Bootstrap.php file

'' => 'main/default/index',
'<_a:(about|contact|captcha)>' => 'main/default/<_a>',

The rules for the module can be for example:

'example' => 'example/default/index', 
'example/<_a:[\w\-]+>' => 'example/default/<_a>',

And then the URL will be as follows:

http://mysite.com/example
http://mysite.com/admin/example

Here so, in general terms)

Creating and connecting the module example in steps

  1. Go to the Gii code generator section Module Generator
    http://yii2-advanced-start.loc/gii/module
    1
  2. Fill the fields and create the module.

Module Class: modules\example\Module
Module ID: example

Click Preview and Generate

4

  1. Add the Bootstrap.php file with the following content

modules\example\Bootstrap.php

<?php

namespace modules\example;

use Yii;

/**
 * Class Bootstrap
 * @package modules\example
 */
class Bootstrap
{
    public function __construct()
    {
        // If there is support for i18n then we set up here
        $i18n = Yii::$app->i18n;
        $i18n->translations['modules/example/*'] = [
            'class' => 'yii\i18n\PhpMessageSource',
            'basePath' => '@modules/example/messages',
            'fileMap' => [
                'modules/example/module' => 'module.php',
            ],
        ];

        // Rules of routing here
        $urlManager = Yii::$app->urlManager;
        $urlManager->addRules(
            [
                // Declaration of rules here
                'example' => 'example/default/index',
                'example/<_a:[\w\-]+>' => 'example/default/<_a>',
            ]
        );
    }
}

bootstrap php

  1. For example, we connect our module to common/config/main.php so it will be available in all applications.
return [
    //...
    'bootstrap' => [
        'modules\example\Bootstrap',
    ],
    'modules' => [
       //...
        'example' => [
            'class' => 'modules\example\Module',
        ],
    ],
    //...
];

common_config_main

http://yii2-advanced-start.loc/example
frontend_demo

http://yii2-advanced-start.loc/admin/example
backend_demo
Done!

Other connections and organization of modules can be viewed already in the installed ones.

Thank`s a lot sir,
Its really great works