用户授权
因为自己的网站不需要用户授权,所以在 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; }