form1.cn
Make a little progress every day

Yii2使用composer安装MongoDB扩展

01th of April 2017 PHP Yii2 8311

注:如果你的PHP不是7的话,要安装2.1.0版本以下的yii2-mongodb

注:不建议手动安装,除非你特别了解手动安装步骤


安装步骤如下

1,确定你的PHP版本,必须安装PHP的MongoDB扩展(请查看本站安装扩展文章:Windows中安装MongoDB的PHP扩展)


2,执行安装

php composer.phar require --prefer-dist yiisoft/yii2-mongodb
或者在composer.json中的require中加入以下代码,然后 composer update
"yiisoft/yii2-mongodb": "^2.0"


3,安装期间会提示输入github提供的Toten

Token (hidden): 4e431ea1fcdf8cd21e8f7dd3f26bc4d373b69f5f 
Token stored successfully.


4,安装成后信息

Package operations: 1 install, 10 updates, 0 removals
  - Updating yiisoft/yii2-composer (2.0.4 => 2.0.5): Downloading (connecting...)
Downloading (100%)
  - Updating swiftmailer/swiftmailer (v5.4.3 => v5.4.6): Downloading (connecting
Downloading (100%)
  - Updating ezyang/htmlpurifier (v4.8.0 => v4.9.2): Downloading (connecting...)
Downloading (100%)
  - Updating cebe/markdown (1.1.0 => 1.1.1): Downloading (100%)
  - Updating bower-asset/jquery.inputmask (3.2.7 => 3.3.4): Downloading (connect
Downloading (100%)
  - Updating yiisoft/yii2 (2.0.9 => 2.0.11.2): Downloading (100%)
  - Updating yiisoft/yii2-swiftmailer (2.0.5 => 2.0.6): Downloading (connecting.
Downloading (100%)
  - Updating imagine/imagine (v0.5.0 => v0.6.3): Downloading (100%)
  - Updating yiisoft/yii2-imagine (2.0.4 => 2.1.0): Downloading (100%)
  - Updating yiisoft/yii2-debug (2.0.6 => 2.0.9): Downloading (100%)
  - Installing yiisoft/yii2-mongodb (2.0.5): Downloading (100%)
Package yiisoft/yii2-codeception is abandoned, you should avoid using it. Use co
deception/codeception instead.
Writing lock file
Generating autoload files


错误处理,明明扩展安装了但还提示找不到

Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - yiisoft/yii2-mongodb 2.1.1 requires ext-mongodb >=1.0.0 -> the requested P
HP extension mongodb is missing from your system.
    - yiisoft/yii2-mongodb 2.1.0 requires ext-mongodb >=1.0.0 -> the requested P
HP extension mongodb is missing from your system.
    - Installation request for yiisoft/yii2-mongodb ~2.1.0 -> satisfiable by yii
soft/yii2-mongodb[2.1.0, 2.1.1].
  To enable extensions, verify that they are enabled in those .ini files:
    - D:\wamp64\bin\php\php5.6.25\php.ini
  You can also run `php --ini` inside terminal to see which files are used by PH
P in CLI mode.

mongo扩展最新版本是2.1,是需要php7支持的,如果你php版本低于7.0,就在composer.json加个`

"yiisoft/yii2-mongodb": "^2.0",`

然后php composer update下。用2.0版本的



从Yii摘录的关于Composer安装与基本使用的文章

如果还没有安装 Composer,你可以按 getcomposer.org 中的方法安装。 在 Linux 和 Mac OS X 中,你可以运行如下命令:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer


在 Windows 中,你需要下载并运行 Composer-Setup.exe。


如果遇到任何问题或者想更深入地学习 Composer, 请参考 Composer 文档(英文),Composer 中文。


如果你已经安装有 Composer 请确保使用的是最新版本, 你可以用 composer self-update 命令更新 Composer 为最新版本。


Composer 安装后,切换到一个可通过 Web 访问的目录,执行如下命令即可安装 Yii :


composer global require "fxp/composer-asset-plugin:^1.2.0"
composer create-project --prefer-dist yiisoft/yii2-app-basic basic


第一条命令安装 Composer asset plugin, 它是通过 Composer 管理 bower 和 npm 包所必须的,此命令全局生效,一劳永逸。 第二条命令会将 Yii 安装在名为 basic 的目录中。如果你想使用其它目录名称,你可以选择其他目录名称。


注意: 在安装过程中 Composer 可能会询问你 GitHub 账户的认证信息,因为可能在使用中超过了 GitHub API (对匿名用户的)使用限制。因为 Composer 需要为所有扩展包从 GitHub 中获取大量信息,所以超限非常正常。(译注:也意味着作为程序猿没有 GitHub 账号,就真不能愉快地玩耍了)登陆 GitHub 之后可以得到更高的 API 限额,这样 Composer 才能正常运行