yii2开发总结(二)

用户受权php

由于本身的网站不须要用户受权,因此在 config/web.php 的配置文件中 把 user 组件给去掉,还要吧  views/layouts/main.php文件里面的相关代码给去掉,在显示页面的时候会提示错误:web

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 相关的代码。json

 

使用MemCache问题缓存

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

Yii1  CCache.phpapp

/**
	 * @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.phpyii

/**
     * 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;
    }
相关文章
相关标签/搜索