duanpi2033 2016-10-12 19:52
浏览 263
已采纳

Yii2:无法处理获取Session

I am using Yii2 Advanced version.

This is Login Model:

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;
}

Also, there is LoginController that I think have no issue, and next thing is when user try to login and session will be opened, and direct to site page. Here is the 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');
    }

Every time I try to login and the Error message comes out and provides:

Invalid Configuration – yii\base\InvalidConfigException

Unexpected configuration type for the "test" component: integer

How to solve the issue, and I try to get $user that stores all the data and it seems to fail?

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,
];
  • 写回答

1条回答 默认 最新

  • doulei6330 2016-10-12 20:41
    关注

    You are using

     Yii::$app->set(self::BACKEND_ID,$this->user['id']);  
    

    probably for set a param value .. but the Class yii\web\Application (alias Yii::$app->set ) contain a function named set() that register component ..(so your error : Unexpected configuration type for the "test" component: integer) in this way your code is misundestood by Yii2 because your costant BACKEND_ID = 'test'; is not a component id but the key for a param

    see this reference for check

    http://www.yiiframework.com/doc-2.0/yii-web-application.html

    http://www.yiiframework.com/doc-2.0/yii-di-servicelocator.html#set()-detail

    for you scope if you need param you can use the file param.php

    returning the param you need

    file config/param.php

    <?php
       return [
         'test' => 'my_initial_value',
    ];
    

    and you can access this param simply using

     \Yii::$app->params['test'], 
    

    or you can simply setting runtime

      \Yii::$app->params['test'] = 'Your_value';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么