2017 © Pedro Peláez
 

yii2-extension yii2-giixer

Extended Yii2 Gii module with common handlers

image

dlds/yii2-giixer

Extended Yii2 Gii module with common handlers

  • Friday, December 8, 2017
  • by dlds
  • Repository
  • 1 Watchers
  • 3 Stars
  • 2,364 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 100 Versions
  • 3 % Grown

The README.md

Yii2 Giixer

Extended gii module for Yii2 including a bunch of useful handler, helpers, traits and other components. This module generates required models, controllers and other classes with dependency on own components. Default yii-gii generator is not available when your are usint yii2-giiixer module., (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

$ composer require dlds/yii2-giixer

or add, (*4)

"dlds/yii2-giixer": "~3.0.0"

to the require section of your composer.json file., (*5)

Migration

There is not any migration required to run. Module itself does not store any data in DB., (*6)

Structure

Giixer defines its own easy to maintain and extendable application structure which is sligtly different from default (gii generated) structure. Below you can find what all and how giixer generates., (*7)

ActiveRecords

Giixer uses its own ActiveRecords (AR) strucutre. Below are all 4 ARs with descriptions about what they stand for. Each is placed on notional level as in application., (*8)

  1. Base AR
    • Top level and not editable AR
    • Maintained only by giixer itself.
    • Always extends GxActiveRecord
    • Manual changes are lost after next giixer generation
    • File is placed in common\models\db\base or common\modules\modulename\models\db\base
  2. Common AR
    • Extends Base AR
    • Editable and maintained by developer
    • Manual changes are not lost after any giixer generation
    • File is placed in common\models\db or common\modules\modulename\models\db
    • Only this model can be directly used by application

Structure schema is shown in this diagram, (*9)

This AR model structure gives you opportunity to easily update your DB schema and still be able to regenerate your AR model without loosing your current code changes., (*10)

Some additional features stored in Base AR is shown in MyModel, (*11)

ActiveQueries

Each AR model is generated with its custom ActiveQuery class which is assigned to Base AR., (*12)

Giixer creates following 3 ActiveQuery (AQ) classes during ARs generation., (*13)

  1. Common AQ
    • Extends \yii\db\ActiveQuery
    • Editable and maintained by developer
    • File is placed in common\models\db\base or common\modules\modulename\models\db\base
    • Always loaded in Base AR (Only this can be directly used by application)

Base AR will automatically loads appropriate AQ., (*14)

Structure schema is shown in this diagram, (*15)

Giixer generates default AQ logic to be able to easily use appropriate AR model class in custom queries., (*16)

Default AQ logic is show in MyModelQuery, (*17)

Helpers

Giixer defines bunch of useful helpers. Below you can see which helpers giixer offers., (*18)

URL Helpers

Own url route and rule helpers are generated for each AR model., (*19)

Giixer creates following 2 helpers (HLP) for both backend and frontend application., (*20)

  1. Route HLP
    • Extends \dlds\giixer\components\helpers\GxRouteHelper or your custom class set in helperRouteBaseClass (see Configuration section)
    • Contains all default routes generated by giixer
    • Directly used by application
    • Frontend file is placed in frontend\components\helpers\url\routes or frontend\modules\modulename\components\helpers\url\routes
    • Backend file is placed in backend\components\helpers\url\routes or backend\modules\modulename\components\helpers\url\routes
  2. Rule HLP
    • Extends \dlds\giixer\components\helpers\GxUrlRuleHelper or your custom class set in helperRuleBaseClass (see Configuration section)
    • Contains rules for all giixer generated routes
    • Usually used in url rules configuration
    • Frontend file is placed in frontend\components\helpers\url\rules or frontend\modules\modulename\components\helpers\url\rules
    • Backend file is placed in backend\components\helpers\url\rules or backend\modules\modulename\components\helpers\url\rules

Main idea of both helpers is to encapsulate rules/routes in single class and provide developer with easy interface for using it., (*21)

Basic route and rule helpers examples: MyBasicRouteHelper, MyBasicUrlRuleHelper, (*22)

To be able to have translatable url slug defined in translation files (i18n) you have to load your rules in application bootstrap like bellow. Otherwise translation will not work. See Yii2 Adding Rules Dynamically, (*23)

class AppBootstrapHandler implements \yii\base\BootstrapInterface {

    public function bootstrap($app)
    {
        return $this->addMainRules($app);
    }

    /**
     * Adds main application rules
     */
    protected function addMainRules(\yii\web\Application $app)
    {
        $rules = require(\Yii::getAlias('@frontend/config/url/rules.php'));

        return $app->getUrlManager()->addRules($rules, false);
    }
}

Where @frontend/config/url/rules.php should look like below., (*24)

$rules = [
    // ...
    MyBasicUrlRuleHelper::index(),
    MyBasicUrlRuleHelper::view(),
    // ...
];

return array_merge(
    $rules, require(__DIR__.'/rules-local.php')
);
?>

Than in application config you just set your AppBootstrapHandler to be processed., (*25)

// ...
'bootstrap' => [
    '\frontend\components\handlers\AppBootstrapHandler',
    // ...
]    
// ...

TIP: Usually you want to have better looking urls without model primary key shown in it. For instance you have own CMS system where each of your Post model has its own slug looks like my-custom-post-title and you want to have final url be like http://www.mydomain.com/my-custom-post-title/. For this case you have to update your UrlRuleHelper class according to MyAdvancedRuleHelper, (*26)

Model Helper

This helper defines methods used in GxActiveRecord class. There are methods for adapting query params to pass mass assignemnt, methods to easily change validation rules or scenario definitions., (*27)

For more information see GxModelHelper, (*28)

Other Helpers

Giixer defines a few another helper class which is more or less complex and useful in basic manipulation with application. Each helper has it own well-documented class., (*29)

For more information see Giixer Helpers, (*30)

Handlers

Giixer defines two main handler class useful for controllers to keep its methods lean and well-arranged., (*31)

  1. GxCrudHandler
    • Defines its own approach of Create, Read, Update, Delete methods
    • Invokes GxCrudEvent during each action which holds all data about action result
    • Usually used for manipulationg standart AR model classes and their appropriate DB entries
  2. GxHandler
    • Defines its own approach to validate model and based on validation result processes appropriate callback
    • Usually used for non AR models manipulation when save() means creating multiple ARs etc...

For more information see Giixer Handlers, (*32)

Configuration

Enable gii module in your config file by adding it to app bootstrap., (*33)

$config['bootstrap'][] = 'gii';

Replace default gii module class with giixer one., (*34)

$config['modules']['gii'] = [
    'class' => 'dlds\giixer\Module',
];

You can also modify giixer module behavior to your requirements by setting additional config options. See bellow., (*35)

namespaces option

Defines namespaces map to generated classes. This is useful if the namespace for some class does not match the default one. For instance if your application is divided into modules and you need to generate classes for these modules., (*36)

[
    '^{Modules}[a-zA-Z]+Form$' => '{module}\models\\forms',
    '^Core[a-zA-Z]+Form$' => 'models\\forms',
    '^{modules}-[a-z-]+$' => '{module}\\views',
]

In example above we can see our application contains modules and also some direct laying "Core" files., (*37)

You can see that we defined also rule for views to be able to place generated view files to appropriate folder., (*38)

There are some special tags you can use when you specifying your rules., (*39)

Special tags
  • {Module} - will be replaced with current module name
  • {Modules} - will be replaced with all modules names regex
  • {module} - will be replaced with current module id
  • {modules} - will be replaced with all modules ids regex

To be able to use these special tags you have to specify your modules names and ids., (*40)

[
    'edu' => 'Edu',
    'shop' => 'Shop',
    // ...
]

Array index is used for modules IDs. Array values for modules Names., (*41)

During generation Giixer will automatically replace these tags so final rules look like bellow (we are generating some Edu model)., (*42)

[
    '^(Edu|Shop){1}[a-zA-Z]+Form$' => 'modules\edu\models\\forms',
    '^Core[a-zA-Z]+Form$' => 'models\\forms',
    '^(edu|shop){1}-[a-z-]+$' => 'modules\edu\\views',
]

bases option

Defines base classes for specific components, controllers and other application classes. It is based on regex of descendant class and fully qualified name of base class., (*43)

[
    BaseHelper::RK_CONTROLLER_BE => [
        '{Modules}[a-zA-Z]+Controller$' => '{module}\\controllers\\base\\{Module}BaseController',
        'Core[a-zA-Z]+Controller$' => 'controllers\\base\\CoreBaseController',
    ],
    BaseHelper::RK_CONTROLLER_FE => [
        '{Modules}[a-zA-Z]+Controller$' => '{module}\\controllers\\base\\{Module}BaseController',
        'Core[a-zA-Z]+Controller$' => 'controllers\\base\\CoreBaseController',
    ],
    // ...
]   

Above you can see configuration for modules controllers which have custom base classes., (*44)

If custom controller (frontend or backend) base class is specified it must extends GxController. Otherwise the GxController will be used directly as parent class. (GxController extends default \yii\web\Controller)., (*45)

If custom route helper base class is set it must extends GxRouteHelper. Otherwise the GxRouteHelper will be used directly as parent class., (*46)

If custom route helper base class is set it must extends GxUrlRuleHelper. Otherwise the GxUrlRuleHelper will be used directly as parent class., (*47)


translations option

Defines which translations files should be automatically generated. This option is defined by array containing languages codes., (*48)

['en', 'de', 'cs']

For above the english, german and czech translations files will be generated., (*49)


messages option

Defines custom translations categories., (*50)

[
    'dynagrid' => [
        '^{Modules}[a-zA-Z]+$' => '{module}/dynagrid',
    ],
],

For above generator use category 'edu/dynagrid' instead of 'dynagrid' everywhere in class which match regex '^Edu[a-zA-Z]+$'., (*51)


For more information see Giixer Module main class, (*52)

Copyright 2016 © Digital Deals s.r.o., (*53)

The Versions

02/11 2016
27/06 2016
21/06 2016
22/03 2016

2.2.7

2.2.7.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

21/03 2016

2.2.6

2.2.6.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

26/02 2016

2.2.5

2.2.5.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

26/02 2016

2.2.4

2.2.4.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

25/02 2016

2.2.3

2.2.3.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

13/01 2016

2.2.2

2.2.2.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

13/01 2016

2.2.1

2.2.1.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

13/01 2016

2.2.0

2.2.0.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

13/01 2016

2.1.9

2.1.9.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

11/01 2016

2.1.8

2.1.8.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

10/01 2016

2.1.7

2.1.7.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

02/01 2016

2.1.6

2.1.6.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

21/12 2015

2.1.5

2.1.5.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

21/12 2015

2.1.4

2.1.4.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

21/12 2015

2.1.3

2.1.3.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

20/12 2015

2.1.2

2.1.2.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

20/12 2015

2.1.1

2.1.1.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

19/12 2015

2.1.0

2.1.0.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

19/12 2015

2.0.9

2.0.9.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

16/12 2015

2.0.8

2.0.8.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

16/12 2015

2.0.7

2.0.7.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

16/12 2015

2.0.6

2.0.6.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

11/12 2015

2.0.5

2.0.5.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

14/10 2015

2.0.4

2.0.4.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

08/10 2015

2.0.3

2.0.3.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

29/09 2015

2.0.2

2.0.2.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

12/05 2015

2.0.1

2.0.1.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

12/05 2015

2.0

2.0.0.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

04/04 2015

1.4.2

1.4.2.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

04/04 2015

1.4.1

1.4.1.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

29/03 2015

1.4

1.4.0.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

28/03 2015

1.3.4

1.3.4.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

22/03 2015

1.3.3

1.3.3.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

22/03 2015

1.3.2

1.3.2.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended

11/03 2015

1.3.1

1.3.1.0

Extended Yii2 Gii module

  Sources   Download

BSD-3-Clause

The Requires

 

by Jiri Svoboda

yii2 gii extended