user-management icon indicating copy to clipboard operation
user-management copied to clipboard

How to customize Controller

Open viniciuseduardo opened this issue 9 years ago • 1 comments

Hello, I'm doing the module implementation and need to do some customizations of models and controllers. But the customizations are not working. can you help me? The following implementations: web.php

'components'=>[
        'user' => [
            'class' => 'webvimark\modules\UserManagement\components\UserConfig',
            'identityClass' => 'app\models\User',
            'loginUrl' => '/login',
            // Comment this if you don't want to record user logins
            'on afterLogin' => function($event) {
                \webvimark\modules\UserManagement\models\UserVisitLog::newVisitor($event->identity->id);
            }
        ],
]
...
    'modules'=>[
        'user-management' => [
            'class' => 'webvimark\modules\UserManagement\UserManagementModule',
            'controllerNamespace' => 'app\controllers',
            'useEmailAsLogin' => true,
            'enableRegistration' => false,
            'emailConfirmationRequired' => false
        ],
    ],

app\models\User.php

<?php

namespace app\models;

use webvimark\modules\UserManagement\models\User as BaseUser;
use yii\helpers\ArrayHelper;
use Yii;
/**
* @property string $name
* @property string $tel_phone
* @property string $cel_phone
*/
class User extends BaseUser{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
            [['name'], 'required'],
            [['name'], 'string', 'max' => 30],
            [['tel_phone'], 'string', 'max' => 13],
            [['cel_phone'], 'string', 'max' => 13]
        ]);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return ArrayHelper::merge(parent::attributeLabels(), [
            'name' => Yii::t('app', 'Nome'),
            'tel_phone'=> Yii::t('app', 'Telefone'),
            'cel_phone'=> Yii::t('app', 'Celular'),
        ]);
    }    

    public function beforeValidate(){
        $this->password = 'ipsense2016';
        $this->repeat_password = 'ipsense2016';

        var_dump($this);die();
        return parent::beforeValidate();
    }
}

app\controllers\UserController.php

<?php

namespace app\controllers;

use webvimark\modules\UserManagement\controllers\UserController as BaseUserController;
use Yii;
use yii\helpers\Url;

class UserController extends BaseUserController{
    public $modelClass = 'app\models\User';
}

Any errors?

viniciuseduardo avatar Nov 11 '15 20:11 viniciuseduardo

One option is to use Yii::$classMap in index.php Another options is to use 'controllerMap' in config.

For example this is how I rewrite controllers (option 1). This is my index.php

<?php
... some code here ...

Yii::$classMap['webvimark\components\BaseController'] = '@app/components/BaseController.php';

(new yii\web\Application($config))->run();

webvimark avatar Nov 18 '15 09:11 webvimark