trance
(Tranceorder)
April 15, 2016, 9:14am
1
My rest controller is extending from ActiveController. I want to do some initialization in my __construct. I have done this:
class BaseController extends ActiveController
{
public function __construct($config = [])
{
// my code
parent::__construct($config);
}
}
I get this error: Missing argument 2 for yii\base\Controller::__construct()
When I look at http://www.yiiframework.com/doc-2.0/yii-rest-activecontroller.html I do not see that overriding constructor require some second params.
btw, my BaseController is inside module.
What I am supposed to do here ?
mdomba
(Maurizio Domba Cerin)
April 15, 2016, 9:28am
2
ActiveController extends yii/rest/Controller that extends yii/web/Controller that in turn extends yii/Base/Controller that has a __construct with 3 parameters
http://www.yiiframework.com/doc-2.0/yii-base-controller.html#__construct()-detail
trance
(Tranceorder)
April 15, 2016, 9:31am
3
Thanks, and how do I get these two: $id, $module ?
trance
(Tranceorder)
April 15, 2016, 9:39am
4
This didn’t worked :
public function __construct($config = [])
{
// code
parent::__construct($this->id, $this->module, $config);
}
mdomba
(Maurizio Domba Cerin)
April 15, 2016, 9:42am
5
Do you get the same error? What is the line of code that fires the error?
trance
(Tranceorder)
April 15, 2016, 9:53am
6
I dont get same error, I dont even know what the real errors is, since I get crazy big messed output that is hard to read. I have spoted PHP warning:
PHP Warning</span>
– <a href="http://www.yiiframework.com/doc-2.0/yii-base-errorexception.html" target="_blank">yii\base\ErrorException</a></h1><h2>Invalid argument supplied for foreach()</h2></div><div class="call-stack"><ul><li class="application call-stack-item"
data-line="9"><div class="element-wrap"><div class="element"><span class="item-number">1.</span><span class="text">in /var/www/html/www.example.com/api/vendor/yiisoft/yii2/BaseYii.php</span><span class="at">
at line <span class="line">520</span></span></div></div><div class="code-wrap"><div class="error-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="hover-line"></div><div class="code"><span class="lines-item">511</span><span class="lines-item">512</span><span class="lines-item">513</span><span class="lines-item">514</span><span class="lines-item">515</span><span class="lines-item">516</span><span class="lines-item">517</span><span class="lines-item">518</span><span class="lines-item">519</span><span class="lines-item">520</span><span class="lines-item">521</span><span class="lines-item">522</span><span class="lines-item">523</span><span class="lines-item">524</span><span class="lines-item">525</span><span class="lines-item">526</span><span class="lines-item">527</span><span class="lines-item">528</span><span class="lines-item">529</span><pre>
My code to aside. Is there any guide that explains how to override constuctor of ActiveController derived class ?
Like: when you want to override __constuct() method of our ActiveController you do this: //explanation.
EDIT: I am doing everything like in this guide: http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html
So I have v1 module and BaseController that extends from ActiveController. And all I want to do is to set some initialization inside constructor of BaseController. Idk why is this so hard.
trance
(Tranceorder)
April 15, 2016, 10:06am
7
I am using init() method to do what I wanted, since I can not make it work in constructor:
public function init()
{
$this->_params = Yii::$app->request->queryParams;
}
mdomba
(Maurizio Domba Cerin)
April 15, 2016, 10:18am
8
yes, I just wanted to suggest init()… it’s a better choice in your case