davidhirtz / yii2-config
Yii2 config module
Package info
github.com/davidhirtz/yii2-config
Type:yii2-extension
pkg:composer/davidhirtz/yii2-config
Requires
- php: ^8.3
- davidhirtz/yii2-skeleton: ^3.8
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.5
- symfony/browser-kit: ^7.4
- symfony/css-selector: ^7.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Makes a project's application params editable in the admin. A project subclasses
Hirtz\Config\Modules\Admin\Models\Config, declares the editable params as attributes with rules and labels,
and the bundle renders them as a form under System. Saving writes the values back into the params file
(config/params.php by default), records a trail entry and updates Yii::$app->params for the running
request. Depends on davidhirtz/yii2-skeleton only.
Installation
composer require davidhirtz/yii2-config
The bundle bootstraps itself through extra.bootstrap (Hirtz\Config\Bootstrap): it registers the @config
alias, the config message category, the config submodule of the admin module and its migration namespace,
and adds Config::AUTH_CONFIG to the roles the admin dashboard offers. Then:
./yii migrate
The migration creates the config permission, granted to the admin and manager roles. The params file
must be writable by the web server; the bundle creates its directory but sets no permissions.
Configuration
Module properties, under modules.admin.modules.config:
| Property | Default | Meaning |
|---|---|---|
configFile |
'@root/config/params.php' |
The params file the form reads and writes, as an alias or path |
defaultRoute |
'config' |
Yii's default controller, so /admin/config opens the form |
The bundle reads no component and no param of its own. The one definition a project sets is the container mapping of the model to its own subclass, plus the subclass itself:
// config/web.php 'container' => [ 'definitions' => [ Hirtz\Config\Modules\Admin\Models\Config::class => App\Models\Config::class, ], ],
// app/Models/Config.php namespace App\Models; class Config extends \Hirtz\Config\Modules\Admin\Models\Config { public array $i18nAttributes = ['claim']; #[\Override] public function rules(): array { return $this->getI18nRules([ [['contactEmail', 'claim'], 'trim'], [['contactEmail'], 'email'], [['claim'], 'string', 'max' => 250], ]); } #[\Override] public function attributeLabels(): array { return [ 'contactEmail' => Yii::t('app', 'Contact email'), 'claim' => Yii::t('app', 'Claim'), ]; } }
What the form shows is activeAttributes(): a param is editable when a validation rule names it, and every
other key in the file survives a save untouched. i18nAttributes clones a field per configured language and
stores claim_de beside claim in the same file; the rules go through getI18nRules(), which is what
declares the _de variant, since nothing on a plain yii\base\Model does it for the subclass.
The admin page
Modules\Admin\Widgets\Navs\ConfigNavItemis added below the skeleton's System nav item, andModule::dashboard()adds a Manage settings item to the dashboard; both point at/admin/config/config/update.Modules\Admin\Controllers\ConfigController::actionUpdate()is guarded byConfig::AUTH_CONFIG(config). The permission is described by the message keyAUTH_CONFIG_DESCRIPTION.Modules\Admin\Widgets\Forms\ConfigActiveFormrenders one row per active attribute, in the orderactiveAttributes()answers, with the file's modification time in the footer; a project changes the rows through aWidget::EVENT_CONFIGURElistener or a subclass overridinggetDefaultRows(), mapped in the container.Modules\Admin\Widgets\Navs\ConfigHeadertitles the page withCONFIG_TITLEand adds the System breadcrumb.- Every save is a trail entry:
ConfigimplementsHirtz\Skeleton\Models\Interfaces\TrailModelInterface, named in the trail bygetAdminType()(CONFIG_NAME).
Console commands
None.