newbrant newbrant - 1 month ago 28
PHP Question

Yii2: Cannot process to get 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,
];

Answer

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