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

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 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?