• Stars
    star
    102
  • Rank 334,566 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Persistent settings in Yii2

Yii2 Settings Extension


Persistent, application-wide settings for Yii2.

Latest Stable Version Total Downloads License Build Status Scrutinizer Code Quality

Support us

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2mod/yii2-settings "*"

or add

"yii2mod/yii2-settings": "*"

to the require section of your composer.json.

Configuration

Database Migrations

Before usage this extension, we'll also need to prepare the database.

php yii migrate --migrationPath=@vendor/yii2mod/yii2-settings/migrations

Module Setup

To access the module, you need to configure the modules array in your application configuration:

'modules' => [
    'settings' => [
        'class' => 'yii2mod\settings\Module',
    ],
],

You can then access settings management section through the following URL:

http://localhost/path/to/index.php?r=settings

or if you have enabled pretty URLs, you may use the following URL:

http://localhost/path/to/index.php/settings

Component Setup

To use the Setting Component, you need to configure the components array in your application configuration:

'components' => [
    'settings' => [
        'class' => 'yii2mod\settings\components\Settings',
    ],
],

Usage:

$settings = Yii::$app->settings;

$value = $settings->get('section', 'key');

$settings->set('section', 'key', 125.5);

$settings->set('section', 'key', 'false', SettingType::BOOLEAN_TYPE);

// Checking existence of setting
$settings->has('section', 'key');

// Activates a setting
$settings->activate('section', 'key');

// Deactivates a setting
$settings->deactivate('section', 'key');

// Removes a setting
$settings->remove('section', 'key');

// Removes all settings
$settings->removeAll();

// Get's all values in the specific section.
$settings->getAllBySection('section');

$settings->invalidateCache(); // automatically called on set(), remove();

Manage custom settings

You can use your own form model to manage custom settings for your web application via SettingsAction. To use the SettingsAction class you need to follow the following steps:

  1. Create your own model, for example:
<?php

namespace app\models\forms;

use Yii;
use yii\base\Model;

class ConfigurationForm extends Model
{
    /**
     * @var string application name
     */
    public $appName;

    /**
     * @var string admin email
     */
    public $adminEmail;

    /**
     * @inheritdoc
     */
    public function rules(): array
    {
        return [
            [['appName', 'adminEmail'], 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(): array
    {
        return [
            'appName' => Yii::t('app', 'Application Name'),
            'adminEmail' => Yii::t('app', 'Admin Email'),
        ];
    }
}
  1. Create view file, named settings.php with the following content:
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $model \app\models\forms\ConfigurationForm */
/* @var $this \yii\web\View */

$this->title = Yii::t('app', 'Manage Application Settings');
?>
<?php $form = ActiveForm::begin(); ?>

<?php echo $form->field($model, 'appName'); ?>

<?php echo $form->field($model, 'adminEmail'); ?>

<?php echo Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>

<?php ActiveForm::end(); ?>
  1. Add settings action to your controller class as follows:
<?php

namespace app\controllers;

use yii\web\Controller;

/**
 * Class SiteController
 *
 * @package app\controllers
 */
class SiteController extends Controller
{
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'manage-settings' => [
                'class' => \yii2mod\settings\actions\SettingsAction::class,
                // also you can use events as follows:
                'on beforeSave' => function ($event) {
                    // your custom code
                },
                'on afterSave' => function ($event) {
                    // your custom code
                },
                'modelClass' => \app\models\forms\ConfigurationForm::class,
            ],
        ];
    }
}

Now you can access to the settings page by the following URL: http://localhost/path/to/index.php?r=site/manage-settings/

Internationalization

All text and messages introduced in this extension are translatable under category 'yii2mod.settings'. You may use translations provided within this extension, using following application configuration:

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'yii2mod.settings' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@yii2mod/settings/messages',
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

More Repositories

1

yii2-comments

Comments module for Yii2
PHP
158
star
2

yii2-rbac

RBAC Manager for Yii 2
PHP
143
star
3

yii2-cart

Yii2 shopping cart
PHP
119
star
4

yii2-swagger

Swagger Documentation Generator for Yii2 Framework
PHP
63
star
5

yii2-enum

Enumerable helper
PHP
63
star
6

yii2-editable

Editable widget and column for gridview.
PHP
54
star
7

base

Yii2 application template
PHP
53
star
8

yii2-cashier

Yii2 Cashier provides an interface to Stripe's subscription billing services.
PHP
45
star
9

yii2-cms

Simple CMS extension
PHP
44
star
10

yii2-sweet-alert

SweetAlert widget for Yii2 framework
PHP
43
star
11

yii2-array-query

Yii2 component that allows for searching/filtering the elements of an array.
PHP
34
star
12

yii2-ftp

FTP Client for Yii2
PHP
33
star
13

collection

Basic collection library for Yii Framework 2.0
PHP
30
star
14

yii2-behaviors

Collection of useful behaviors for Yii Framework 2.0
PHP
29
star
15

yii2-user

Flexible user registration and authentication module for Yii2.
PHP
27
star
16

yii2-link-preview

LinkPreview widget render page preview
PHP
27
star
17

yii2-image

Provides methods for the dynamic manipulation of images. Various image formats such as JPEG, PNG, and GIF can be resized, cropped, rotated.
PHP
26
star
18

yii2-cron-log

Component for logging cron jobs
PHP
23
star
19

yii2-ion-slider

Easily customizable range slider with skins support.
PHP
22
star
20

yii2-tree

Tree widget based on Fancytree extension
PHP
21
star
21

yii2-selectize

selectize.js wrapper for yii2.
PHP
19
star
22

yii2-google-maps-markers

Google Maps Markers Widget for Yii2
PHP
18
star
23

yii2-star-rating

Star rating widget based on jQuery Raty
PHP
18
star
24

yii2-validators

Collection of useful validators for Yii Framework 2.0
PHP
17
star
25

yii2-scheduling

Scheduling extension for Yii2 framework
PHP
17
star
26

yii2-helpers

Collection of useful helper functions for Yii Framework 2.0
PHP
15
star
27

yii2-timezone

Timezone detector
PHP
15
star
28

yii2-bootstrap-notify

Bootstrap Notify widget for Yii2 framework
PHP
14
star
29

yii2-chosen-select

Select Widget based on Chosen jQuery plugin
PHP
14
star
30

yii2-markdown

Markdown Widget for Yii 2
PHP
13
star
31

yii2-c3-chart

Yii2 wrapper for D3-based reusable chart library
PHP
11
star
32

yii2-bx-slider

bx-slider.js wrapper for yii2.
PHP
11
star
33

yii2-braintree

Yii2 Braintree provides an interface to Braintree subscription billing services.
PHP
9
star
34

yii2-gii-extended

This generator generates enumerable classes or controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified data model.
PHP
8
star
35

yii2-moderation

A simple Content Moderation System for Yii2
PHP
7
star
36

yii2-disqus

Yii2 disqus comment widget
PHP
6
star
37

yii2-feed

social feeds widget
PHP
6
star
38

yii2-pie

d3pie.js wrapper for yii2
PHP
5
star
39

yii2-toggle-column

Provides a toggle data column and action for Yii Framework 2.0
PHP
3
star