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;
    }
相關文章
相關標籤/搜索