YiiRewriteMap.php 重写没有效果

FecMall问题咨询 · boluo · 于 4年前 发布 · 1646 次阅读

通过@appserver/config/YiiRewriteMap.php重写model。但是没有效果。不知道是不是姿势不对的问题。求指点.

appserver/config/YiiRewriteMap.php

<?php

return [
    /**
     * \fecshop\models\mongodb\Category 为原来的类
     * \appfront\local\local_models\mongodb\Category 为重写后的类
     * 重写后的类可以集成原来的类。
     */
    // '\fecshop\models\mongodb\Category'  => '\appfront\local\local_models\mongodb\Category',
    '\fecshop\models\mysqldb\customer\CustomerLogin' =>
    '\appserver\local\local_models\mysqldb\customer\CustomerLogin',
];
appserver\local\local_models\mysqldb\customer\CustomerLogin.php

<?php
namespace appserver\local\local_models\mysqldb\customer;

use Yii;

class CustomerLogin extends fecshop\models\mysqldb\customer\CustomerLogin {

    public function rules()
    {
        return [
            [['email', 'password'], 'required'],
            // ['email', 'email'],
            ['password', 'validatePassword'],
        ];
    }

}
共收到 12 条回复 问题提问
boluo#14年前 0 个赞

为什么图片没有显示。。。

Fecmall#24年前 0 个赞

1.你添加配置文件:appserver/config/YiiRewriteMap.php

那么针对的,只有appserver入口,其他的入口不生效,如果想要全部生效,需要写道common中

2.model文件:appserver\local\local_models\mysqldb\customer\CustomerLogin.php

修改为:

class CustomerLogin extends \fecshop\models\mysqldb\customer\CustomerLogin {

也就是fecshop字符前面添加 \,但是你没有生效,意思应该是没有报错,因此,我判断你的配置应该没有加载

你写的格式:

'\appserver\local\local_models\mysqldb\customer\CustomerLogin',

没有问题,加载问题,参看回复1,猜测你访问的入口不是appserver

3.仔细看一下文件路径是否正确,以及大小写问题。

boluo#34年前 0 个赞

入口是server没错,斜杠加和没有加都试过了,但是还是不行.不知道到底是什么原因.

Fecmall#44年前 0 个赞

提供一下信息,你自己debug一下试试吧

1.customer services: https://github.com/fecshop/yii2_fecshop/blob/master/services/Customer.php#L64

2.重写的函数在这里: https://github.com/fecshop/yii2_fecshop/blob/master/services/Customer.php#L87

 list($this->_customerLoginModelName, $this->_customerLoginModel) = Yii::mapGet($this->_customerLoginModelName);

也就是Yii::mapGet方法

https://github.com/fecshop/yii2_fecshop/blob/master/yii/Yii.php#L34

public static function mapGet($absoluteClassName, $arguments = []){
        $absoluteClassName = self::mapGetName($absoluteClassName);
        if (!empty($arguments) && is_array($arguments)) {
            $class = new ReflectionClass($absoluteClassName);
            $absoluteOb = $class->newInstanceArgs($arguments);
            /**
             * 下面的 ...,是php的语法糖(只能php5.6以上,放弃),也就是把$paramArray数组里面的各个子项参数,
             *  作为对象生成的参数,详细可以参看:https://segmentfault.com/q/1010000006789348
             */
            //$absoluteOb = new $absoluteClassName(...$arguments);
        } else {
            $absoluteOb = new $absoluteClassName;
        }
        
        return [$absoluteClassName, $absoluteOb];
    }

自己debug一下,找找原因吧

boluo#54年前 0 个赞
<?php

// ...

$yiiClassMap = [];
$yiiClassMap = array_merge($yiiClassMap, require(__DIR__ . '/../../common/config/YiiClassMap.php'));
$yiiClassMap = array_merge($yiiClassMap, require(__DIR__ . '/YiiClassMap.php'));

$fecRewriteMap = [];
$fecRewriteMap = array_merge($fecRewriteMap, require(__DIR__ . '/../../common/config/YiiRewriteMap.php'));
$fecRewriteMap = array_merge($fecRewriteMap, require(__DIR__ . '/YiiRewriteMap.php'));

return [
    'modules'  => $modules,
    'services' => $services,
    'components' => $components,
    'yiiClassMap' => $yiiClassMap,
    'fecRewriteMap' => $fecRewriteMap,
    'params' => [
        'rateLimit'             => [
            'enable'=> false,   // 是否开启?默认不开启速度控制。
            'limit' => [120, 60], // 速度控制[120,60] 代表  60秒内最大访问120次,
        ]
    ],
];

我最后是在fecshop_local.php文件中添加了对YiiRewriteMap.php的使用实现了重写.作者你是不是没有使用YiiRewriteMap.php文件.

Fecmall#64年前 0 个赞

@boluo [[[#5楼](#comment5)](#comment5)](#comment5) 看了您的回复,我回去查看了一下代码,是我搞错了

现在已经转移到 @app/config/fecshop_local.php文件里面配置,例子:https://github.com/fecshop/yii2_fecshop_app_advanced/blob/master/appfront/config/fecshop_local.php

当时这样修改的原因,是fecmall-2版本需要做应用市场,扩展的要求是,可以重写所有的功能,可以通过配置覆盖的方式进行重写,因此RewriteMap也在这个里面,因此进行了重构,由于当时的疏漏,文档忘记更新了,本人也忘记这个了,造成上面的回复出现问题,上面的回复针对的fecmall-1版本,fecmall-2版本需要写到@app/confog/fecshop_local.php配置文件里面,譬如:https://github.com/fecshop/yii2_fecshop_app_advanced/blob/master/appfront/config/fecshop_local.php

如果是应用插件扩展重写,可以参看:http://www.fecmall.com/topic/2216 ,这个里面有一个部分:

// yii class rewrite map
                'yiiClassMap' => [
                    // 'fecshop\app\appfront\helper\test\My' => '@appfront/helper/My.php',
                ],
                // 重写model和block
                'fecRewriteMap' => [
                    '\fecshop\app\appfront\modules\Catalog\block\category\Index'  => '\fecyo\app\appfront\modules\Catalog\block\category\Index',
                    // '\fecshop\app\appfront\modules\Customer\block\address\Edit'  => '\fectb\app\appfront\modules\Customer\block\address\Edit',
                ],

感谢您花费时间研究这个问题,本人将更新一下文件(fecshop_local.php里面加上例子,删除YiiRewrite.php文件),以及更新文档。

再次感谢!

Fecmall#94年前 0 个赞

OK,此问题已完善解决。

boluo#104年前 0 个赞

作者很用心,解决速度很快.点赞

Fecmall#114年前 0 个赞

@boluo #10楼 彼此彼此。

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics