Yii2: How to add JavaScript library through composer?

I installed the base application according to this guide (Installing Yii) . It's not a problem. According to the manual, I also added fxp / composer-asset-plugin globally to composer.phar. Also no problem.

Now I have a requirement to work with q.js , which is hosted * as an npm package . But I do not know how to add it through the composer. I know that I could perhaps use the CDN instead, or load and save it manually. But I prefer to use a composer. So what do I need to do to make this work?

I added this to my composer.json:

"require": {
    "php": ">=5.4.0",
    "yiisoft/yii2": ">=2.0.4",
    "yiisoft/yii2-bootstrap": "*",
    "yiisoft/yii2-swiftmailer": "*",
    "npm-asset/q": "~1.4"               <------
},

and called composer.phar update, but received an exception:

Your requirements could not be resolved to an installable set of packages.

    Problem 1
    - The requested package npm-asset/q could not be found in any version, 
      there may be a typo in the package name.

?

, JS- . , Asset, JS .

* ?

+4
1

- / AppAsset.php.

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $js = [
        'js\your_file_name.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

- js , js .

js , :

$this->registerJsFile(Yii::$app->request->baseUrl.'/js/your_file_name.js');
0

All Articles