newbrant newbrant - 1 year ago 179
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->user = $user;

public function login(){
return false;
return true;


private function createSession(){

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()
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?


$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 Source

You are using


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

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

returning the param you need

file config/param.php

   return [
     'test' => 'my_initial_value',

and you can access this param simply using


or you can simply setting runtime

  \Yii::$app->params['test'] = 'Your_value';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download