2017 © Pedro Peláez
 

yii2-extension yii2-module-autoloader

Extension to autoload modules dynamically

image

bmsrox/yii2-module-autoloader

Extension to autoload modules dynamically

  • Saturday, April 14, 2018
  • by bmsrox
  • Repository
  • 2 Watchers
  • 5 Stars
  • 267 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 22 % Grown

The README.md

YII2 Module AutoLoader

INSTALL

composer require bmsrox/yii2-module-autoloader

or, (*1)

"bmsrox/yii2-module-autoloader":"dev-master"

HOW TO USE

Create a module in your app and add config.php in the module root path, (*2)

config.php

use app\modules\admin\AdminModule;

return [
    'id' => 'admin',
    'class' => AdminModule::className(),
    'urlManagerRules' => [
        '/admin' => '/admin/default/index'
    ]
];

Set the components in your web.php or main.php., (*3)

'components' => [
        ...
        'moduleLoader' => [
            'class' => 'bmsrox\autoloader\ModuleLoader',
            'modules_paths' => [
                '@backend/modules', 
                '@frontend/modules', 
                '@common/modules'
                ]
        ],
        ...
 ]

PS: If you are using a basic template the default modules_paths is @app/modules. but you can specify any path., (*4)

Set the bootstrap as, (*5)

'bootstrap' => [
    ...
    'moduleLoader'
    ...
 ],

Using Events

Example to use a events, (*6)

I've been created an event called SidebarMenu to add menu dynamically when i create a new module., (*7)

use yii\base\Component;

class SidebarMenu extends Component {

    const REGISTER = 'register';

    private $items = [];

    public function init() {
        $this->trigger(self::REGISTER);
        return parent::init();
    }

    public function setItem($item) {
        if (!isset($item['sortOrder']))
            $item['sortOrder'] = 1000;
        $this->items[] = $item;
    }

    public function getItem() {
        $this->sortItems();
        return $this->items;
    }

    /**
     * Sorts the item attribute by sortOrder
     */
    private function sortItems() {
        usort($this->items, function ($a, $b) {
            if ($a['sortOrder'] == $b['sortOrder']) {
                return 0;
            } else
            if ($a['sortOrder'] < $b['sortOrder']) {
                return - 1;
            } else {
                return 1;
            }
        });
    }

}

Create a class Events in your module root path like, (*8)

    use yii\base\Object;
    use yii\helpers\Html;

    class Events extends Object {

        public static function onMenuRegister($event) {
            $event->sender->setItem([
                'label' => 'example',
                'url' => ['/example/default/index'],
                'visible' => !Yii::$app->user->isGuest,
                'sortOrder' => 2
            ]);
        }

    }

In your module/config.php add a key into array config, (*9)

    'events' => [
        ['class' => SidebarMenu::className(), 'event' => SidebarMenu::REGISTER, 'callback' => [Events::className(), 'onMenuRegister']],
    ],

Call the Menu class to render a dynamic menu, (*10)

        echo Menu::widget([
            'items' => (new \app\components\SidebarMenu())->getItem(),
        ]);

So you can add many events into your module that it will be added automatically., (*11)

The Versions

14/04 2018

dev-master

9999999-dev https://github.com/bmsrox/yii2-module-autoloader

Extension to autoload modules dynamically

  Sources   Download

BSD-3-Clause

The Requires

 

by Bruno Marinho

extension yii2 modules autoload bmsrox

14/04 2018

v1.1.1

1.1.1.0 https://github.com/bmsrox/yii2-module-autoloader

Extension to autoload modules dynamically

  Sources   Download

BSD-3-Clause

The Requires

 

by Bruno Marinho

extension yii2 modules autoload bmsrox

10/05 2017

v1.1.0

1.1.0.0 https://github.com/bmsrox/yii2-module-autoloader

Extension to autoload modules dynamically

  Sources   Download

BSD-3-Clause

The Requires

 

by Bruno Marinho

extension yii2 modules autoload bmsrox

30/11 2016

v1.0.0

1.0.0.0 https://github.com/bmsrox/yii2-module-autoloader

Extension to autoload modules dynamically

  Sources   Download

BSD-3-Clause

The Requires

 

by Bruno Marinho

extension yii2 modules autoload bmsrox