mix8872/yii2-config

Module for store config in db

Installs: 616

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

3.0.0 2023-04-14 12:40 UTC

This package is auto-updated.

Last update: 2024-04-14 14:55:19 UTC


README

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist mix8872/config

or add

"mix8872/yii2-config": "dev-master"

to the require section of your composer.json.

Configuration

Edit components section of your application config file.

Common:

'components' => [
    'config' => [
        'class' => 'mix8872\config\components\Config'
    ],

// other components
]

Edit controllerMap and modules section of your application config file.

Backend:

'controllerMap' => [
    'elfinder' => [
        'class' => 'mihaildev\elfinder\Controller',
        'access' => ['admin'], //глобальный доступ к фаил менеджеру @ - для авторизорованных , ? - для гостей , чтоб открыть всем ['@', '?']
        'disabledCommands' => ['netmount'], //отключение ненужных команд https://github.com/Studio-42/elFinder/wiki/Client-configuration-options#commands
        'roots' => [
            [
                'baseUrl'=>'@web',
                'basePath'=>'@webroot',
                'path' => 'uploads',
                'name' => 'Uploads',
                'options' => [
                    'uploadOverwrite' => false,
                    'uploadAllow' => ['*'],
                    // 'uploadDeny' => ['pdf'],
                    'uploadOrder' => ['allow', 'deny'],
                    'uploadMaxSize' => '50M',
                    'disabled' => ['mkfile'],
                ],
            ]
        ]
    ],
    
    // other controllers
],
],
'modules' => [
    'config' => [
        'class' => 'mix8872\config\Module',
        'adminRole' => 'admin', // optional, defines all rights on options editing for role
        'as access' => [
            'class' => 'yii\filters\AccessControl',
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['admin']
                ],
            ]
        ]
    ],
    'gridview' =>  [
        'class' => '\kartik\grid\Module'
        // enter optional module parameters below - only if you need to
        // use your own export download action or custom translation
        // message source
        // 'downloadAction' => 'gridview/export/download',
        // 'i18n' => []
    ],
    
    // other modules
]

If declared adminRole then other user which haven't declared permissions can't create

Next run migration:

yii migrate --migrationPath=@vendor/mix8872/yii2-config/src/migrations

Usage

Now you can open url \config\ and manage you params

To get param value:

  • Yii::$app->config-><param_key>
  • or Yii::$app->config->g('param_key')

To set param value from code:

  • Yii::$app->config-><param_key> = <param_value>
  • or Yii::$app->config->s(<param_key>, <param_value>)
  • or Yii::$app->config->s('<param_key>,<param_value>')
  • or Yii::$app->config->s([<param_key>,<param_value>])
  • or Yii::$app->config->s([<param_key> => <param_value>])

Events

Config module has next events:

  • EVENT_AFTER_CREATE - fires on config option was added
  • EVENT_AFTER_UPDATE - fires on config option was updated
  • EVENT_AFTER_DELETE - fires on config option was deleted
  • EVENT_BEFORE_SAVE - fires on each config option was saved

You can catch this events in config this way:

'modules' => [
    'config' => [
        'class' => 'mix8872\config\Module',
        'on ' . \mix8872\config\Module::EVENT_AFTER_CREATE => function ($e) {
            $model = $e->model;
            // do something
        },
        'on ' . \mix8872\config\Module::EVENT_AFTER_UPDATE => function ($e) {
            $model = $e->model;
            // do something
        },
        'on ' . \mix8872\config\Module::EVENT_AFTER_DELETE => function ($e) {
            $model = $e->model;
            // do something
        },
        'on ' . \mix8872\config\Module::EVENT_BEFORE_SAVE => function ($e) {
            $model = $e->model;
            // do something
        },
    ]
]

In $e->model the event passes an object (or array of objects) of config items.

In EVENT_BEFORE_SAVE event the model is passed by reference
and can be modified from event handler.

Access rules

You can define access rules for config options by define adminRole in config
or set rules inside config options. Access matrix you can find here.