set param in _construct

I have doubt about how to set variable in __construct

I have some controllers, each controller has a js file, so when I used Yii 1, I created a variable in construct and to set a address to js, I do this:




class MyController extends Controller{

    public $scriptJS;

    

    public function __construct($id) {


        parent::$this->scriptJS = array(Yii::app()->request->baseUrl . "/libs/myjs/myjs.js");

        parent::__construct($id);

    }

..

}



After put this code in construct I put this code in my default layout:




<?php if (isset($this->scriptJS) AND !empty($this->scriptJS)): ?>

            <?php foreach ($this->scriptJS as $script) : ?>

                <script src="<?= $script ?>"></script> 

            <?php endforeach; ?>

        <?php endif; ?>



This way each controller load the specific js, I’m trying do this in Yii 2, I created this construct:




public $scriptJS;

    

    public function __construct($id, $module, $config = []) {

        $this->id = $id;

        $this->module = $module;        

        $this->scriptJS = array("/libs/myjs/myjs.js");

        parent::__construct($id, $module, $config = []);

    }



And in my default layout I put the same code:




<?php if (isset($this->scriptJS) AND !empty($this->scriptJS)): ?>

            <?php foreach ($this->scriptJS as $script) : ?>

                <script src="<?= $script ?>"></script> 

            <?php endforeach; ?>

        <?php endif; ?>



But don’t load the variable scriptJS. Where I’m wrong?

Yii2 is not backwards compatible with 1.x. The good news is that what you want to do is now simpler, you could achieve the same thing in a constructor or init method with:





$this->scriptJS = array(Yii::app()->request->baseUrl . "/libs/myjs/myjs.js");

foreach($this->scriptJS as $script)

{

       $this->registerJsFile($script);

}




Thanks for help,

So I put this in my model construct:




public function __construct() {

        $this->scriptJS = array(Yii::$app->request->baseUrl . "/libs/myjs/myjs.js");

        foreach($this->scriptJS as $script)

        {

               $this->registerJsFile($script);

        }

    }




But I receive this error:

[b]

Unknown Method – yii\base\UnknownMethodException

Calling unknown method: app\controllers\UsersController::registerJsFile()

[/b]

the method is unknown. Is this the correct way?

I read this guide:

http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail

So I think that I used the correct way, but I received this error message. How can I fix? I need that each model use a specific js file

[b]

Calling unknown method: app\controllers\RisksController::registerJsFile()

1. in /Users/Alexandre/web_projects/ibless/medicine_work/vendor/yiisoft/yii2/base/Component.php at line 282


273274275276277278279280281282283284285286287288289290291





     */


    public function __call(&#036;name, &#036;params)


    {


        &#036;this-&gt;ensureBehaviors();


        foreach (&#036;this-&gt;_behaviors as &#036;object) {


            if (&#036;object-&gt;hasMethod(&#036;name)) {


                return call_user_func_array([&#036;object, &#036;name], &#036;params);


            }


        }


        throw new UnknownMethodException('Calling unknown method: ' . get_class(&#036;this) . &quot;::&#036;name()&quot;);


    }


 


    /**


     * This method is called after the object is created by cloning an existing one.


     * It removes all behaviors because they are attached to the old object.


     */


    public function __clone()


    {


        &#036;this-&gt;_events = [];

[/b]

Hello

I managed to solve the problem, I need to set a javascript file to each controller, so I created in each controller a variable :

public $scriptJS = "/js/myjs1.js";

And in my main layout I put this:

<script type="text/javascript" src="<?= Yii::$app->controller->scriptJS ?>"></script>

This resolved my problem.

But if there is the better way please put here