Dominus77 / yii2-advanced-start

Yii2 Start Project Advanced Template
https://dominus77.github.io/yii2-advanced-start/
MIT License
23 stars 12 forks source link

How to add new modules #23

Closed agussudarmanto closed 6 years ago

agussudarmanto commented 6 years ago

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.

Dominus77 commented 6 years ago

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: https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/modules/main/Module.php#L29 https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/modules/main/Module.php#L45 And files views: https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/modules/main/Module.php#L46 https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/modules/main/Module.php#L48
  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 https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/backend/config/main.php#L17 https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/backend/config/main.php#L22-L24 common/config/main.php https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/common/config/main.php#L11-L13 frontend/config/main.php https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/frontend/config/main.php#L43

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 https://github.com/Dominus77/yii2-advanced-start/blob/826bb3aed5622c27a0620edac8e2d2f562944313/modules/main/Bootstrap.php#L30-L31

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)

Dominus77 commented 6 years ago

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.

agussudarmanto commented 6 years ago

Thank`s a lot sir, Its really great works