2016-01-04 15:33:07 6678次浏览 5条回答 1 悬赏 10 金钱
public function __construct($id, $module, $config = [])
{
    $this->id = $id;
    $this->module = $module;
    parent::__construct($config);
}

上面的代码是我看到了它在yii\base\Controller中定义了这个函数,如果我需要在某个控制器中初始化一些数据该怎么做?
比如

class DemoController extends Controller
{
    protected $request;
    public function __construct(){
         parent::__construct();
         $this->request=\Yii::$app->request;
     }
}

想要有这样的功能,却直接报错了

  • 回答于 2016-01-04 16:01 举报

    直接用 不用构造了

  • 回答于 2016-01-04 17:40 举报

    重写方法 init();
    这个是controller继承object开放出来初始化的。
    因为所有的都继承object,所以所有的都可以用init初始化。

  • 回答于 2016-01-05 07:48 举报

    可以写在init() function中

  • 回答于 2016-01-06 22:20 举报

    首先,可以使用init()方法,其次如果非要重载__construct()的话,你的写法错了哦

    应该是

    public function __construct($id, $module, $config = [])
    {
        parent::__construct($id, $module, $config);
        $this->request=\Yii::$app->request;
    }
    
    
  • 回答于 2016-01-08 10:22 举报

    楼上 是对的

您需要登录后才可以回答。登录 | 立即注册
foryoufeng
实习生

foryoufeng

注册时间:2015-12-31
最后登录:2016-02-24
在线时长:0小时59分
  • 粉丝0
  • 金钱0
  • 威望0
  • 积分0

热门问题