我使用的是Yii2高级版。
这是登录模型:
namespace common\models;
use Yii;
use yii\base\Model;
use common\models\User;
class LoginForm extends Model{
public $username;
public $password;
public $rememberMe = true;
public $verifyCode;
const BACKEND_TEST = 'none';
const BACKEND_ID = 'test';
const BACKEND_USERNAME = 'backend_username';
private $user;
public function rules(){
return [ [['username','password'],'required','message'=>'{attribute}required...'],
['username','validateUser'], ['verifyCode','captcha','captchaAction'=>'login/captcha','message'=>'Wrong'],
];
}
public function validateUser($attribute,$params){
$user = User::findOne(['username'=>$this->username]);
if(!$user || (md5($this->password) != $user['password'])){
$this->addError('password','Wrong>_<...');
}else{
$this->user = $user;
}
}
public function login(){
if(!$this->user){
return false;
}
var_dump($this->userInfo());
$this->createSession();
return true;
}
private function createSession(){
//Yii::$app->session->open();
Yii::$app->set(self::BACKEND_ID,$this->user['id']);
Yii::$app->set(self::BACKEND_USERNAME,$this->user['username']);
}
public function userInfo(){
return $this->user;
}
另外,我认为有一个LoginController没有问题,接下来就是当用户尝试登录时会打开会话并直接进入网站页面。 这是sitecontroller:
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
/**
* Site controller
*/
class SiteController extends Controller
{
public function actionIndex()
{
//var_dump(Yii::$app->session->get(common\models\LoginForm::BACKEND_ID));
var_dump(LoginForm::userInfo());
return $this->renderPartial('index');
}
每次我尝试登录并出现错误消息并提供:
无效配置 - yii \ base \ InvalidConfigException
“test”组件的意外配置类型:整数
如何解决问题,我尝试获取存储所有数据的$ user,但似乎失败了?
main.php:
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => ['smister' => [
'class' => 'backend\modules\smister\smister',
],],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'params' => $params,
];
答案 0 :(得分:0)
您正在使用
Yii::$app->set(self::BACKEND_ID,$this->user['id']);
可能用于设置参数值.. 但是类yii \ web \ Application(别名Yii :: $ app-&gt; set)包含一个名为set()的函数来注册组件..(所以你的错误:&#34; test&#34的意外配置类型;组件:整数)这样你的代码被Yii2误解了,因为你的costant BACKEND_ID =&#39; test&#39 ;;不是组件ID,而是param的关键
请参阅此参考资料
http://www.yiiframework.com/doc-2.0/yii-web-application.html
http://www.yiiframework.com/doc-2.0/yii-di-servicelocator.html#set()-detail
对于您的范围,如果您需要param,则可以使用文件param.php
返回你需要的参数
file config / param.php
<?php
return [
'test' => 'my_initial_value',
];
您只需使用
即可访问此参数 \Yii::$app->params['test'],
或者您可以简单地设置运行时
\Yii::$app->params['test'] = 'Your_value';