Overriding Classes

This module makes extensive use of the Yii2's Dependency Injection Container. The module has a special attribute named classMap where it allows you to override specific classes.

The following are the classes that you can override throughout that attribute:

How to Override

The classMap contains an easy to recognize name and their correspondent definition. The default configuration can be seen at the Bootstrap class:

$defaults = [
    // --- models
    'User' => 'Da\User\Model\User',
    'SocialNetworkAccount' => 'Da\User\Model\SocialNetworkAccount',
    'Profile' => 'Da\User\Model\Profile',
    'Token' => 'Da\User\Model\Token',
    'Assignment' => 'Da\User\Model\Assignment',
    'Permission' => 'Da\User\Model\Permission',
    'Role' => 'Da\User\Model\Role',
    // --- search
    'UserSearch' => 'Da\User\Search\UserSearch',
    'PermissionSearch' => 'Da\User\Search\PermissionSearch',
    'RoleSearch' => 'Da\User\Search\RoleSearch',
    // --- forms
    'RegistrationForm' => 'Da\User\Form\RegistrationForm',
    'ResendForm' => 'Da\User\Form\ResendForm',
    'LoginForm' => 'Da\User\Form\LoginForm',
    'SettingsForm' => 'Da\User\Form\SettingsForm',
    'RecoveryForm' => 'Da\User\Form\RecoveryForm',
    // --- services
    'MailService' => 'Da\User\Service\MailService',

As you can see, the only thing we need to do is actually modify its definition. For example, the following configuration will override the RegistrationForm class:

namespace app\forms;

use Da\User\Form\RegistrationForm as BaseForm;

class RegistrationForm extends BaseForm {

     * Override from parent
    public function rules() {
        // your logic

Now, to tell the module to use your class instead, you simply need to update the definition of that class into the the Module::classMap attribute.

// ...

'modules' => [
    'user' => [
        'class' => Da\User\Module::class,
        'classMap' => [
            'RegistrationForm' => 'app\forms\RegistrationForm'

The definition can be any of the following (from Yii2's DI container):

See how to enhance a User model guide to see a practical example.

© 2amigos 2013-2019