logics – 永夜 https://www.shuijingwanwq.com 没有不值得去解决的问题,也没有不值得去学习的技术! Sun, 17 May 2026 09:38:01 +0000 zh-Hans hourly 1 https://wordpress.org/?v=7.0 在 Yii 2.0 上,声明与需验证模型特性相关的验证规则,使用了[[yii\behaviors\TimestampBehavior]],重写 [[yii\base\Model::rules()]] 方法的技巧 https://www.shuijingwanwq.com/2018/05/30/2671/ https://www.shuijingwanwq.com/2018/05/30/2671/#respond Wed, 30 May 2018 07:47:47 +0000 http://www.shuijingwanwq.com/?p=2671 浏览量: 98 1、/common/models 目录中的模型类文件仅允许Gii工具所生成,为公共的模型数据层,如图1
/common/models 目录中的模型类文件仅允许Gii工具所生成,为公共的模型数据层

图1



    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['group_id', 'config_column_id', 'plan_id', 'title', 'config_task_id', 'create_user_id', 'create_name', 'exec_user_id', 'exec_name', 'task_data', 'ended_at', 'created_at', 'updated_at'], 'required'],
            [['config_column_id', 'plan_id', 'sort_order', 'config_task_id', 'create_user_id', 'exec_user_id', 'ended_at', 'current_step_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['task_info', 'task_data'], 'string'],
            [['group_id', 'create_name', 'exec_name'], 'string', 'max' => 32],
            [['title'], 'string', 'max' => 64],
            [['place'], 'string', 'max' => 255],
        ];
    }


2、/common/logics 目录中的模型类文件为业务逻辑相关,继承至 /common/models 数据层,为公共的模型逻辑层,验证规则为场景相关的规则与公共的模型数据层的规则,在必填项中删除 created_at, updated_at


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            /* 指派(创建任务) */
            [ [ 'plan_id', 'title', 'config_task_id', 'exec_user_id'], 'required', 'on' => 'create' ], //create
            [ 'config_task_id', 'exist', 'targetClass' => '\common\logics\ConfigTask', 'targetAttribute' => 'id', 'filter' => [ 'status' => ConfigTask::CONFIG_TASK_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ 'plan_id', 'exist', 'targetClass' => '\common\logics\Plan', 'targetAttribute' => 'id', 'filter' => [ 'status' => Plan::PLAN_STATUS_PASSED ], 'on' => 'create' ], //create
            [ 'create_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'create_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ 'exec_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'exec_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ [ 'place', 'task_info' ], 'default', 'value' => '', 'on' => 'create' ], //create
            /* 我的任务(获取任务列表) */
            [ [ 'status' ], 'in', 'range' => [ self::PLAN_TASK_STATUS_ALL, self::PLAN_TASK_STATUS_NOT_BEGINNING, self::PLAN_TASK_STATUS_BEGINNING, self::PLAN_TASK_STATUS_DONE ], 'on' => 'index' ], //index
            [ [ 'config_task_id' ], 'in', 'range' => array_merge([ ConfigTask::CONFIG_TASK_STATUS_ALL ], ConfigTask::find()->select('id')->where([ 'status' => ConfigTask::CONFIG_TASK_STATUS_ENABLE ])->asArray()->column()), 'on' => 'index' ], //index
            [ [ 'plan_id' ], 'default', 'value' => 0, 'on' => 'index' ], //index
            [ [ 'status', 'config_task_id' ], 'default', 'value' => self::PLAN_TASK_STATUS_ALL, 'on' => 'index' ], //index
            /* 认领(认领任务) */
            [ 'id', 'exist', 'targetClass' => '\common\logics\PlanTask', 'targetAttribute' => [ 'id', 'exec_user_id' ], 'filter' => [ 'status' => self::PLAN_TASK_STATUS_NOT_BEGINNING ], 'on' => 'claim' ], //claim
            /* 转派(转派任务) */
            [ [ 'exec_user_id' ], 'required', 'on' => 'transfer' ], //transfer
            [ 'exec_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'exec_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'transfer' ], //transfer
            [ 'id', 'exist', 'targetClass' => '\common\logics\PlanTask', 'filter' => [ 'status' => self::PLAN_TASK_STATUS_NOT_BEGINNING ], 'on' => 'transfer' ], //transfer
            [['group_id', 'config_column_id', 'plan_id', 'title', 'config_task_id', 'create_user_id', 'create_name', 'exec_user_id', 'exec_name', 'task_data', 'ended_at'], 'required'],
            [['config_column_id', 'plan_id', 'sort_order', 'config_task_id', 'create_user_id', 'exec_user_id', 'ended_at', 'current_step_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['task_info', 'task_data'], 'string'],
            [['group_id', 'create_name', 'exec_name'], 'string', 'max' => 32],
            [['title'], 'string', 'max' => 64],
            [['place'], 'string', 'max' => 255],
        ];


3、公共的模型逻辑层,验证规则方法调整如下,如图2
公共的模型逻辑层,验证规则方法调整如下

图2




use yii\helpers\ArrayHelper;

class PlanTask extends \common\models\PlanTask
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        $rules = [
            /* 指派(创建任务) */
            [ [ 'plan_id', 'title', 'config_task_id', 'exec_user_id'], 'required', 'on' => 'create' ], //create
            [ 'config_task_id', 'exist', 'targetClass' => '\common\logics\ConfigTask', 'targetAttribute' => 'id', 'filter' => [ 'status' => ConfigTask::CONFIG_TASK_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ 'plan_id', 'exist', 'targetClass' => '\common\logics\Plan', 'targetAttribute' => 'id', 'filter' => [ 'status' => Plan::PLAN_STATUS_PASSED ], 'on' => 'create' ], //create
            [ 'create_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'create_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ 'exec_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'exec_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'create' ], //create
            [ [ 'place', 'task_info' ], 'default', 'value' => '', 'on' => 'create' ], //create
            /* 我的任务(获取任务列表) */
            [ [ 'status' ], 'in', 'range' => [ self::PLAN_TASK_STATUS_ALL, self::PLAN_TASK_STATUS_NOT_BEGINNING, self::PLAN_TASK_STATUS_BEGINNING, self::PLAN_TASK_STATUS_DONE ], 'on' => 'index' ], //index
            [ [ 'config_task_id' ], 'in', 'range' => array_merge([ ConfigTask::CONFIG_TASK_STATUS_ALL ], ConfigTask::find()->select('id')->where([ 'status' => ConfigTask::CONFIG_TASK_STATUS_ENABLE ])->asArray()->column()), 'on' => 'index' ], //index
            [ [ 'plan_id' ], 'default', 'value' => 0, 'on' => 'index' ], //index
            [ [ 'status', 'config_task_id' ], 'default', 'value' => self::PLAN_TASK_STATUS_ALL, 'on' => 'index' ], //index
            /* 认领(认领任务) */
            [ 'id', 'exist', 'targetClass' => '\common\logics\PlanTask', 'targetAttribute' => [ 'id', 'exec_user_id' ], 'filter' => [ 'status' => self::PLAN_TASK_STATUS_NOT_BEGINNING ], 'on' => 'claim' ], //claim
            /* 转派(转派任务) */
            [ [ 'exec_user_id' ], 'required', 'on' => 'transfer' ], //transfer
            [ 'exec_user_id', 'exist', 'targetClass' => '\common\logics\ConfigColumnUser', 'targetAttribute' => [ 'exec_user_id' => 'user_id', 'config_column_id' => 'config_column_id' ], 'filter' => [ 'status' => ConfigColumnUser::CONFIG_COLUMN_USER_STATUS_ENABLE ], 'on' => 'transfer' ], //transfer
            [ 'id', 'exist', 'targetClass' => '\common\logics\PlanTask', 'filter' => [ 'status' => self::PLAN_TASK_STATUS_NOT_BEGINNING ], 'on' => 'transfer' ], //transfer
        ];
        $parentRules = parent::rules();
        return ArrayHelper::merge($rules, $parentRules);
    }
}


4、由于使用了 [[yii\behaviors\TimestampBehavior]],这个行为支持在 [[yii\db\ActiveRecord|Active Record]] 存储时自动更新它的时间戳属性,因此需要在验证规则中去掉:created_at, updated_at,设置其默认值为0,如图3
由于使用了 [[yii\behaviors\TimestampBehavior]],这个行为支持在 [[yii\db\ActiveRecord|Active Record]] 存储时自动更新它的时间戳属性,因此需要在验证规则中去掉:created_at, updated_at,设置其默认值为0

图3

5、/common/models 目录中的模型类文件仅允许Gii工具所生成,为公共的模型数据层,生成的验证规则:created_at, updated_at,已经不是必填项


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['group_id', 'config_column_id', 'plan_id', 'title', 'config_task_id', 'create_user_id', 'create_name', 'exec_user_id', 'exec_name', 'task_data', 'ended_at'], 'required'],
            [['config_column_id', 'plan_id', 'sort_order', 'config_task_id', 'create_user_id', 'exec_user_id', 'ended_at', 'current_step_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['task_info', 'task_data'], 'string'],
            [['group_id', 'create_name', 'exec_name'], 'string', 'max' => 32],
            [['title'], 'string', 'max' => 64],
            [['place'], 'string', 'max' => 255],
        ];
    }


]]>
https://www.shuijingwanwq.com/2018/05/30/2671/feed/ 0
基于 yiisoft/yii2-app-advanced,在 GitHub 上新建仓库 yii2-app-advanced,新建接口应用(实现 RESTful 风格的 Web Service 服务的 API),实现模型分层:数据层、逻辑层,明确公共目录、应用、模块的继承、引用关系 https://www.shuijingwanwq.com/2018/04/04/2559/ https://www.shuijingwanwq.com/2018/04/04/2559/#respond Wed, 04 Apr 2018 07:19:26 +0000 http://www.shuijingwanwq.com/?p=2559 浏览量: 1,232

1、定义与规范:
定义:
(1)数据层:models 用于定义数据相关的自动验证和自动完成和数据存取接口;
(2)逻辑层:logics 用于定义数据相关的业务逻辑;

规范:
(1)/common/models 目录中的模型类文件仅允许Gii工具所生成,为公共的模型数据层;
(2)/common/logics 目录中的模型类文件为业务逻辑相关,继承至 /common/models 数据层,为公共的模型逻辑层;
(3)/common 目录中需要引用模型类文件,仅引用 /common/logics 中的模型类文件,例:


public $modelClass = 'common\logics\User';


(4)/api/models、/backend/models、/frontend/models 目录中的模型类文件为业务逻辑相关(仅与各应用相关),继承至 /common/logics 公共逻辑层;
(5)接口、前端、后端应用目录中需要引用模型类文件,仅引用各自目录下的对应模型类文件,例:


use api\models\User;


(6)/api/modules/v1/models 目录中的模型类文件为业务逻辑相关(仅与模块相关),继承至 /api/models 应用逻辑层;
(7)模块目录中需要引用模型类文件,仅引用各自模块下的对应模型类文件,例:


public $modelClass = 'api\modules\v1\models\User';


2、在common目录中新建logics目录,用于MySQL模型的逻辑层所在目录,如图1

在common目录中新建logics目录,用于MySQL模型的逻辑层所在目录

图1

3、复制 \common\models\LoginForm.php、\common\models\User.php 至 \common\logics,如图2

复制 \common\models\LoginForm.php、\common\models\User.php 至 \common\logics

图2

4、在common/models目录中的MySQL模型文件仅为Gii工具所生成,删除 \common\models\LoginForm.php,如图3

在common/models目录中的MySQL模型文件仅为Gii工具所生成,删除 \common\models\LoginForm.php

图3

5、配置路由,使用美观的 URL ,编辑 \frontend\config\main.php


        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],


6、国际化的支持,新建目录 \common\messages\en-US\model、\common\messages\zh-CN\model,分别用于模型的数据层的美国英语、中文简体;新建 \common\messages\{language}\app.php、\common\messages\{language}\error.php、\common\messages\{language}\success.php,分别用于应用全局消息、应用错误消息、应用成功消息,如图4

国际化的支持,新建目录 \common\messages\en-US\model、\common\messages\zh-CN\model,分别用于模型的数据层的美国英语、中文简体;新建 \common\messages\{language}\app.php、\common\messages\{language}\error.php、\common\messages\{language}\success.php,分别用于应用全局消息、应用错误消息、应用成功消息

图4


app.php

<?php
return [
];

error.php

<?php
return [
    20000 => 'error',
];

success.php

<?php
return [
    10000 => 'success',
];

7、配置应用程序语言,源语言为美国英语,目标语言为简体中文,当源语言和目标语言相同时,是否强制进行消息翻译,默认为假,设置为真,编辑 \common\config\main.php


    'sourceLanguage' => 'en-US',
    'language' => 'zh-CN',
    'components' => [
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'forceTranslation' => true,
                    'basePath'=>'@common/messages',
                    'fileMap' => [
                        'app' => 'app.php',
                        'error' => 'error.php',
                        'success' => 'success.php',
                    ],
                ],
            ],
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],


8、打开网址:http://www.github-shuijingwan-yii2-app-advanced.localhost/gii/model ,选项,命名空间为common\models,此时需支持国际化,覆盖\common\models\User.php,如图5

打开网址:http://www.github-shuijingwan-yii2-app-advanced.localhost/gii/model ,选项,命名空间为common\models,此时需支持国际化,覆盖\common\models\User.php

图5

<?php
 
namespace common\models;
 
use Yii;
 
/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }
 
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
            [['status', 'created_at', 'updated_at'], 'integer'],
            [['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['username'], 'unique'],
            [['email'], 'unique'],
            [['password_reset_token'], 'unique'],
        ];
    }
 
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('model/user', 'ID'),
            'username' => Yii::t('model/user', 'Username'),
            'auth_key' => Yii::t('model/user', 'Auth Key'),
            'password_hash' => Yii::t('model/user', 'Password Hash'),
            'password_reset_token' => Yii::t('model/user', 'Password Reset Token'),
            'email' => Yii::t('model/user', 'Email'),
            'status' => Yii::t('model/user', 'Status'),
            'created_at' => Yii::t('model/user', 'Created At'),
            'updated_at' => Yii::t('model/user', 'Updated At'),
        ];
    }
}
 
 

9、在common/logics目录中的MySQL模型文件为业务逻辑相关,继承至 \common\models 数据层,打开网址:http://www.github-shuijingwan-yii2-app-advanced.localhost/gii/model ,选项,如图6

在common/logics目录中的MySQL模型文件为业务逻辑相关,继承至 \common\models 数据层,打开网址:http://www.github-shuijingwan-yii2-app-advanced.localhost/gii/model ,选项
图6

10、基于 diff ,编辑 \common\logics\User.php,如图7

11、在common/logics目录中的MySQL模型文件为业务逻辑相关,继承至 \common\models\User 数据层

<?php
 
namespace common\logics;
 
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\web\IdentityInterface;
 
/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 * @property string $password write-only password
 */
class User extends \common\models\User implements IdentityInterface
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
 
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }
 
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'auth_key', 'password_hash', 'email'], 'required'],
            [['status', 'created_at', 'updated_at'], 'integer'],
            [['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['username'], 'unique'],
            [['email'], 'unique'],
            [['password_reset_token'], 'unique'],
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
        ];
    }
 
    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
    }
 
    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }
 
    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
    }
 
    /**
     * Finds user by password reset token
     *
     * @param string $token password reset token
     * @return static|null
     */
    public static function findByPasswordResetToken($token)
    {
        if (!static::isPasswordResetTokenValid($token)) {
            return null;
        }
 
        return static::findOne([
            'password_reset_token' => $token,
            'status' => self::STATUS_ACTIVE,
        ]);
    }
 
    /**
     * Finds out if password reset token is valid
     *
     * @param string $token password reset token
     * @return bool
     */
    public static function isPasswordResetTokenValid($token)
    {
        if (empty($token)) {
            return false;
        }
 
        $timestamp = (int) substr($token, strrpos($token, '_') + 1);
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
        return $timestamp + $expire >= time();
    }
 
    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }
 
    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }
 
    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }
 
    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return bool if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }
 
    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }
 
    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }
 
    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
    }
 
    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }
}
 

12、新建 \common\messages\en-US\model\user.php,支持目标语言为英语美国时的消息翻译

<?php
/**
 * Created by PhpStorm.
 * User: WangQiang
 * Date: 2018/04/04
 * Time: 10:34
 */
 
return [
    'ID' => 'ID',
    'Username' => 'Username',
    'Auth Key' => 'Auth Key',
    'Password Hash' => 'Password Hash',
    'Password Reset Token' => 'Password Reset Token',
    'Email' => 'Email',
    'Status' => 'Status',
    'Created At' => 'Created At',
    'Updated At' => 'Updated At',
];

13、新建 \common\messages\zh-CN\model\user.php,支持目标语言为简体中文时的消息翻译

<?php
/**
 * Created by PhpStorm.
 * User: WangQiang
 * Date: 2018/04/04
 * Time: 10:39
 */
 
return [
    'ID' => 'ID',
    'Username' => '用户名',
    'Auth Key' => '认证密钥',
    'Password Hash' => '密码哈希',
    'Password Reset Token' => '密码重置令牌',
    'Email' => '邮箱',
    'Status' => '状态',
    'Created At' => '创建时间',
    'Updated At' => '更新时间',
];

14、新建 \api\models\User.php,在api/models目录中的MySQL模型文件为业务逻辑相关(仅与api相关),继承至 \common\logics\User 逻辑层

<?php
/**
 * Created by PhpStorm.
 * User: WangQiang
 * Date: 2018/04/04
 * Time: 10:44
 */
 
namespace api\models;
 
class User extends \common\logics\User
{
 
}

15、复制\api\models\User.php 至 \frontend\models\User.php、\backend\models\User.php,调整为各自的命名空间
\frontend\models\User.php

<?php
/**
 * Created by PhpStorm.
 * User: WangQiang
 * Date: 2018/04/04
 * Time: 10:44
 */
 
namespace frontend\models;
 
class User extends \common\logics\User
{
 
}

\backend\models\User.php

<?php
/**
 * Created by PhpStorm.
 * User: WangQiang
 * Date: 2018/04/04
 * Time: 10:44
 */
 
namespace backend\models;
 
class User extends \common\logics\User
{
 
}

16、在 api 应用中搜索 use common\models\User;,替换为:use api\models\User;,在前台、后台应用中同样类似处理,如图8

在 api 应用中搜索 use common\models\User;,替换为:use api\models\User;,在前台、后台应用中同样类似处理

图8

17、在 api 应用中搜索 common\models\User,替换为:api\models\User,在前台、后台应用中同样类似处理,如图9

在 api 应用中搜索 common\models\User,替换为:api\models\User,在前台、后台应用中同样类似处理

图9

18、编辑 \common\logics\LoginForm.php,调整命名空间

<?php
namespace common\logics;
 
use Yii;
use yii\base\Model;
 
/**
 * Login form
 */
class LoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;
 
    private $_user;
 
 
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }
 
    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, 'Incorrect username or password.');
            }
        }
    }
 
    /**
     * Logs in a user using the provided username and password.
     *
     * @return bool whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
        }
         
        return false;
    }
 
    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    protected function getUser()
    {
        if ($this->_user === null) {
            $this->_user = User::findByUsername($this->username);
        }
 
        return $this->_user;
    }
}
 
 

19、新建 \api\models\LoginForm.php,在api/models目录中的MySQL模型文件为业务逻辑相关(仅与api相关),继承至 \common\logics\LoginForm 逻辑层

<?php
/**
 * Created by PhpStorm.
 * User: WangQiang
 * Date: 2018/04/04
 * Time: 11:10
 */
 
namespace api\models;
 
 
class LoginForm extends \common\logics\LoginForm
{
 
}

20、复制\api\models\LoginForm.php 至 \frontend\models\LoginForm.php、\backend\models\LoginForm.php,调整为各自的命名空间
\frontend\models\LoginForm.php

<?php
/**
 * Created by PhpStorm.
 * User: WangQiang
 * Date: 2018/04/04
 * Time: 11:10
 */
 
namespace frontend\models;
 
 
class LoginForm extends \common\logics\LoginForm
{
 
}

\backend\models\LoginForm.php

<?php
/**
 * Created by PhpStorm.
 * User: WangQiang
 * Date: 2018/04/04
 * Time: 11:10
 */
 
namespace backend\models;
 
 
class LoginForm extends \common\logics\LoginForm
{
 
}

21、在 api 应用中搜索 use common\models\LoginForm;,替换为:use api\models\LoginForm;,在前台、后台应用中同样类似处理,如图10

在 api 应用中搜索 use common\models\LoginForm;,替换为:use api\models\LoginForm;,在前台、后台应用中同样类似处理

图10

22、在 api 应用中搜索 common\models\LoginForm,替换为:api\models\LoginForm,在前台、后台应用中同样类似处理,如图11

在 api 应用中搜索 common\models\LoginForm,替换为:api\models\LoginForm,在前台、后台应用中同样类似处理

图11

23、在 common 目录中搜索 common\models\User,替换为:common\logics\User,除了 \common\logics\User.php 例外,如图12

在 common 目录中搜索 common\models\User,替换为:common\logics\User,除了 \common\logics\User.php 例外

图12

24、在 common 目录中搜索 common\models\LoginForm,替换为:common\logics\LoginForm,如图13

在 common 目录中搜索 common\models\LoginForm,替换为:common\logics\LoginForm

图13

25、测试注册功能,打开网址:http://www.github-shuijingwan-yii2-app-advanced.localhost/site/signup ,符合预期,如图14

测试注册功能,打开网址:http://www.github-shuijingwan-yii2-app-advanced.localhost/site/signup ,符合预期

图14

26、查看数据库,如图15

查看数据库

图15

 

]]>
https://www.shuijingwanwq.com/2018/04/04/2559/feed/ 0
在 Yii 2.0 高级模板 中实现 模型分层:数据层、逻辑层 的流程 https://www.shuijingwanwq.com/2017/08/15/1713/ https://www.shuijingwanwq.com/2017/08/15/1713/#respond Tue, 15 Aug 2017 07:10:48 +0000 http://www.shuijingwanwq.com/?p=1713 浏览量: 117

1、定义:
数据层:models 用于定义数据相关的自动验证和自动完成和数据存取接口。
逻辑层:logics 用于定义数据相关的业务逻辑。

2、在common目录中新建logics目录,用于MySQL模型的逻辑层所在目录,如图1

在common目录中新建logics目录,用于MySQL模型的逻辑层所在目录

图1

3、在common/logics目录中新建redis目录,用于Redis(ActiveRecord)模型的逻辑层所在目录,如图2

在common/logics目录中新建redis目录,用于Redis(ActiveRecord)模型的逻辑层所在目录

图2

4、在common/models目录中的MySQL模型文件为Gii工具所生成,如图3

在common/models目录中的MySQL模型文件为Gii工具所生成

图3

5、在common/models/redis目录中的Redis(ActiveRecord)模型文件为模型字段结构的定义,如图4

在common/models/redis目录中的Redis(ActiveRecord)模型文件为模型字段结构的定义

图4

6、在common/logics目录中的MySQL模型文件为业务逻辑相关,继承至 \common\models\Game 数据层,如图5

api\models
common\logics

在common/logics目录中的MySQL模型文件为业务逻辑相关,继承至 \common\models\Game 数据层

图5

7、在common/logics/redis目录中的Redis(ActiveRecord)模型文件为业务逻辑相关,继承至 \common\models\redis\Game 数据层,如图6

api\models\redis
common\logics\redis

api\models
common\models

Api
Common

common\models\redis\Lock
common\logics\redis\Lock

common\models\redis\Live
common\logics\redis\Live

$api
$common

在common/logics/redis目录中的Redis(ActiveRecord)模型文件为业务逻辑相关,继承至 \common\models\redis\Game 数据层

图6

8、在common/logics/redis目录中的Redis(原生命令)模型文件为业务逻辑相关,不需要继承至 \common\models\redis\Game 数据层,而是直接继承至 \yii\redis\ActiveRecord,如图7

在common/logics/redis目录中的Redis(原生命令)模型文件为业务逻辑相关,不需要继承至 \common\models\redis\Game 数据层,而是直接继承至 \yii\redis\ActiveRecord

图7

9、在api/models目录中的MySQL模型文件为业务逻辑相关,继承至 \common\logics\Game 逻辑层,如图8

\common\models
\common\logics

在api/models目录中的MySQL模型文件为业务逻辑相关,继承至 \common\logics\Game 逻辑层

图8

10、在api/models/redis目录中的Redis(ActiveRecord/原生命令)模型文件为业务逻辑相关,继承至 \common\logics\redis\Game 逻辑层,如图9

common\models\redis\Lock
common\logics\redis\Lock

\common\models\redis
\common\logics\redis

在api/models/redis目录中的Redis(ActiveRecord/原生命令)模型文件为业务逻辑相关,继承至 \common\logics\redis\Game 逻辑层

图9

]]>
https://www.shuijingwanwq.com/2017/08/15/1713/feed/ 0