在yii2中,如果js  css文件等不存在,会到vendor/bower文件夹下面找,找到后加载到当前目录的web下面

如果我们的yii2的扩展需要用到js css,就需要加入js css这些文件

方法如下:

1.在github下面开一个项目:譬如:

https://github.com/zqy234/my.js

2.在http://bower.io/ 中进行注册

首先在linux 下面执行

yum install npm

sudo npm install -g bower

linux 设置sudo可以到网络搜索如何设置

安装log如下:

[terry@iZ942k2d5ezZ ~]$ sudo npm install -g bower
[sudo] password for terry: 
npm http GET https://registry.npmjs.org/bower
npm http 304 https://registry.npmjs.org/bower
npm http GET https://registry.npmjs.org/semver-utils
npm http 304 https://registry.npmjs.org/semver-utils
/usr/bin/bower -> /usr/lib/node_modules/bower/bin/bower
npm WARN unmet dependency /usr/lib/node_modules/block-stream requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream-ignore requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream-npm requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/glob requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/npmconf requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/tar requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
bower@1.7.2 /usr/lib/node_modules/bower
?..?€ semver-utils@1.1.1
[terry@iZ942k2d5ezZ ~]$ 

创建bower.json

bower init
[terry@iZ942k2d5ezZ tt]$ bower init
? name terry
? description water
? main file my.js
? what types of modules does this package expose? 
? keywords js
? authors terry
? license MIT
? homepage https://github.com/zqy234/my.js
? set currently installed components as dependencies? Yes
? add commonly ignored files to ignore list? Yes
? would you like to mark this package as private which prevents it from being accidentally published to the registry? No

{
  name: 'terry',
  description: 'water',
  main: 'my.js',
  moduleType: [],
  keywords: [
    'js'
  ],
  authors: [
    'terry'
  ],
  license: 'MIT',
  homepage: 'https://github.com/zqy234/my.js',
  ignore: [
    '**/.*',
    'node_modules',
    'bower_components',
    'test',
    'tests'
  ],
  dependencies: {
    'my.js': 'https://github.com/zqy234/my.js.git#~1.0.0'
  }
}

? Looks good? Yes
[terry@iZ942k2d5ezZ tt]$ ls
bower_components  bower.json

完成后里面有一个bower.json文件,吧这个文件上传到github对应的项目目录 下面

3注册到bower

bower register terryjs git://github.com/zqy234/my.js.git

[terry@iZ942k2d5ezZ tt]$ bower register terryjstest  git://github.com/zqy234/my.js.git
bower terryjstest#*            resolve git://github.com/zqy234/my.js.git#*
bower terryjstest#*           download https://github.com/zqy234/my.js/archive/1.0.0.tar.gz
bower terryjstest#*            extract archive.tar.gz
bower terryjstest#*       invalid-meta terryjstest is missing "main" entry in bower.json
bower terryjstest#*       invalid-meta terryjstest is missing "ignore" entry in bower.json
bower terryjstest#*           resolved git://github.com/zqy234/my.js.git#1.0.0
? Registering a package will make it installable via the registry (https://bower.herokuapp.com), continue? Yes
bower terryjstest             register git://github.com/zqy234/my.js.git

Package terryjstest registered successfully!
All valid semver tags on git://github.com/zqy234/my.js.git will be available as versions.
To publish a new version, just release a valid semver tag.

Run bower info terryjstest to list the available versions.
[terry@iZ942k2d5ezZ tt]$

查看信息:
[terry@iZ942k2d5ezZ tt]$ bower info terryjstest
bower terryjstest#*             cached git://github.com/zqy234/my.js.git#1.0.0
bower terryjstest#*           validate 1.0.0 against git://github.com/zqy234/my.js.git#*

{
  name: 'terryjs',
  homepage: 'https://github.com/zqy234/my.js',
  version: '1.0.0'
}

Available versions:
  - 1.0.0

You can request info for a specific version with 'bower info terryjstest#<version>'
[terry@iZ942k2d5ezZ tt]$ 


搜索:

[terry@iZ942k2d5ezZ tt]$ bower search terryjstest
Search results:

    terryjstest git://github.com/zqy234/my.js.git
[terry@iZ942k2d5ezZ tt]$ 

然后可以在

http://bower.io/search/

中搜索到

譬如搜索:http://bower.io/search/?q=terryjstest

就找到我关联的包了。

4.在文件中加入依赖:

{
  "name": "zqy234/logtest",
  "description": "Yii2 zqy234 test",
  "keywords": [
    "yii2",
    "logtest"
  ],
  "homepage": "https://github.com/zqy234/logtest",
  "type": "yii2-extension",
  "license": "MIT",
  "support": {
    "source": "https://github.com/zqy234/logtest"
  },
  "authors": [
    {
      "name": "terry water",
      "email": "zqy234@126.com"
    }
  ],
  "minimum-stability": "stable",
  "require": {
    "php": ">=5.4.0",
    "yiisoft/yii2": ">=2.0.6",
    "bower-asset/terryjstest": "~1.0"
   
  },
  "autoload": {
    "psr-4": {
      "terry\\": ""
    }
  },
  "config": {
    "process-timeout": 1800
  }
}

5

到yii跟目录下执行:

[root@iZ942k2d5ezZ fancyecommerce.com]# composer require --prefer-dist zqy234/logtest
Using version ^1.2 for zqy234/logtest
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing bower-asset/terryjstest (1.0.0)             
    Downloading: 100%         

  - Removing zqy234/logtest (1.0.0)
  - Installing zqy234/logtest (1.2.0)
    Downloading: 100%         

Writing lock file
Generating autoload files
[root@iZ942k2d5ezZ fancyecommerce.com]#

[root@iZ942k2d5ezZ fancyecommerce.com]# ls vendor/bower/
bootstrap    jquery  jquery.inputmask  punycode  terryjstest  typeahead.js  yii2-pjax

可以看到terryjstest被加载到vendor/bower包下面了、


注意:在执行前,需要先运行asset的插件:

composer global require "fxp/composer-asset-plugin:~1.1.1"

















Logo

更多推荐