Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to mainLayoutAsset.php file?

Tags:

yii2

I have following code in mainLayoutAsset.php file

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace frontend\assets;
use yii\web\AssetBundle;

/**
 * @author Qiang Xue <[email protected]>
 * @since 2.0
 */
class MainLayoutAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
    ];
    public $js = [
        'member-area/AdminLTE/app.js',
    ];
}

Now I want to access params file parameter in to this file

eg.

public $js = [
        'member-area/AdminLTE/app.js?v='.Yii::$app->params["version"],
     ]

but it giving error

PHP Parse Error – yii\base\ErrorException
syntax error, unexpected '.', expecting ']'

'js/tooltip.js?v='.Yii::$app->params["incFileVersion"],
like image 395
Prashant Patil Avatar asked Jun 23 '16 12:06

Prashant Patil


1 Answers

From PHP documentation about class properties :

They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

http://php.net/manual/en/language.oop5.properties.php

You could simply override init() :

public function init()
{
    parent::init();
    $this->js = [
        'member-area/AdminLTE/app.js?v=' . Yii::$app->params['version'],
    ];
}

And you should may be try this instead.

like image 66
soju Avatar answered Nov 16 '22 10:11

soju