yii2开发总结(二)

用户授权

因为自己的网站不需要用户授权,所以在 config/web.php 的配置文件中 把 user 组件给去掉,还要吧  views/layouts/main.php文件里面的相关代码给去掉,在显示页面的时候会提示错误:

An Error occurred while handling another error:
exception 'yii\base\InvalidConfigException' with message 'User::identityClass must be set.' in /yii2/2.0.9/yiisoft/yii2/web/User.php:163
echo Nav::widget([
        'options' => ['class' => 'navbar-nav navbar-right'],
        'items' => [
            ['label' => 'Home', 'url' => ['/site/index']],
            ['label' => 'About', 'url' => ['/site/about']],
            ['label' => 'Contact', 'url' => ['/site/contact']],
        ],
    ]);

去掉 有关  app->user 相关的代码。

 

使用MemCache问题

由于之前的工程使用yii1 里面的memcache, 新工程同样需要使用缓存,也就使用了MemCache. 发现新工程里面无法访问旧工程设置的缓存内容,反之亦然。配置的memcache是同一个。最后通过比较代码,发现yii1中CCache.php 和 yii2中 Cache.php 文件中生成缓存key的规则发生了变化。代码如下:

Yii1  CCache.php

/**
	 * @param string $key a key identifying a value to be cached
	 * @return string a key generated from the provided key which ensures the uniqueness across applications
	 */
	protected function generateUniqueKey($key)
	{
		return $this->hashKey ? md5($this->keyPrefix.$key) : $this->keyPrefix.$key;
	}

Yii2 Cache.php

/**
     * Builds a normalized cache key from a given key.
     *
     * If the given key is a string containing alphanumeric characters only and no more than 32 characters,
     * then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key
     * is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]].
     *
     * @param mixed $key the key to be normalized
     * @return string the generated cache key
     */
    public function buildKey($key)
    {
        if (is_string($key)) {
            $key = ctype_alnum($key) && StringHelper::byteLength($key) <= 32 ? $key : md5($key);
        } else {
            $key = md5(json_encode($key));
        }

        return $this->keyPrefix . $key;
    }

 

转载于:https://my.oschina.net/skyzwg/blog/733959

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值