Shahneel Ahmed Shahneel Ahmed - 27 days ago 10
PHP Question

Call to undefined method Yii::import()

Getting Error Call to undefined method Yii::import()
As i am trying to access my Yii controllers action i am getting this error please
guide me who do i access and where i am going wrong now
my code is

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\controllers\SSPController;


class BrandController extends Controller
{
public function actionIndex()
{

Yii::import('application.controllers.admin.SSPController');
SSPController::getdata();

}
}


my SSPController Class Code is Like from which i want to call method getdata()

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SSPController extends Controller
{


static function getdata(){

$primaryKey = 'user_id';
$table = 'gc_od_chat';
$columns_names=$arrayName = array("user_email","user_username");
$columns = array(
array( 'db' => 'columns_names[0]', 'dt' => 'columns_names[0]', 'field' => 'columns_names[0]' ),
array( 'db' => 'columns_names[1]', 'dt' => 'columns_names[1]', 'field' => 'columns_names[1]' ),
);

$sql_details = array(
'user' =>"root",
'pass' => "",
'db' => "octachat",
'host' => "localhost"
);

echo json_encode(
$this->simple( $_GET, $sql_details, $table, $primaryKey, $columns, $joinQuery, $extraWhere )
);



}
}

Answer

There is no Yii::import() in Yii 2 but there are many ways to achieve this.

For example:

  • make BrandController extend SSPController so it can use SSPController's methods
  • call Yii::$app->runAction('controller/action', $params); (in case of action methods)