使用Composer安装部署、升级Drupal8及Drupal Console和Drush的使用

admin 2019-03-17 drupal composer 1578

1.Drupal默认安装(下载)

composer create-project drupal-composer/drupal-project:8.x-dev my_site_name_dir --no-interaction

注意修改my_site_name_dir为你的项目名称。

使用此命令会自动安装drupal console和drush

2.Drupal自定义安装(下载)

第一步:(注意修改my_site_name_dir为你的项目名称)

composer create-project drupal-composer/drupal-project:8.x-dev my_site_name_dir --stability dev --no-interaction --no-install

第二步:根据你的需求编辑 composer.json,比如可以修改下载路径中web(默认网站根目录)为你想要的路径;

第三步:运行以下命令即可自动下载源码和依赖包。

composer install

3.如果你的Drupal站点以前没有用Composer管理,想配置为用composer管理,那么,参考执行以下命令:

cd path-to-drupal-project-repo(一般用drupal根目录的上一级目录)
composer global require grasmash/composerize-drupal
composer composerize-drupal --composer-root=[repo-root] --drupal-root=[drupal-root]

 [composer-root] 为项目目录,[drupal-root] 为Drupal站点根目录,一般[drupal-root] 为 [composer-root]的子目录。 

示例:

# Drupal is located in a `docroot` subdirectory.
composer composerize-drupal --composer-root=. --drupal-root=./docroot

# Drupal is located in a `web` subdirectory.
composer composerize-drupal --composer-root=. --drupal-root=./web

# Drupal is located in a `public_html` subdirectory (cPanel compatible).
composer composerize-drupal --composer-root=. --drupal-root=./public_html

# Drupal is located in the repository root, not in a subdirectory.
composer composerize-drupal --composer-root=. --drupal-root=.

其他可选项:

--no-update: 不修改composer.json
--no-gitignore: 不修改 .gitignore文件

这个命令有时似乎是跟不上drupal的版本更新,怎么办?可以加群194666179说明,我们将提供已经配置好的composer配置文件(包含最新版的drupal composer和几个常用模块配置),你只需要把相关文件直接放到你的drupal目录的上一级目录即可实现用composer管理你的drupal。

4.使用Composer下载模块和主题

#下载最新版
composer require drupal/<modulename>
#示例:
composer require drupal/token
#指定版本
composer require 'drupal/<modulename>:<version>'
#示例
composer require 'drupal/token:^1.5'
composer require 'drupal/simple_fb_connect:~3.0'
composer require 'drupal/ctools:3.0.0-alpha26'
composer require 'drupal/token:1.x-dev'

5.使用Composer升级核心或组件

#查看是否有可用升级
composer outdated "drupal/*"
#升级核心
composer update drupal/core --with-dependencies
drush updatedb
drush cr

升级指定名称模块、主题:

#注意:引号不可省略,否则无法检测升级
composer update "drupal/modulename" --with-dependencies
drush updatedb
drush cr

升级核心、所有模块、主题:

composer update drupal/* --with-dependencies
drush updatedb
drush cr

6.关于Drush的安装

composer require drush/drush

7.关于Drupal Console的安装

如果没有用1和2的方法,则默认没有drupal console,需要通过以下方式安装

composer require drupal/console:~1.0 --prefer-dist --optimize-autoloader

Drupal Console的升级

composer update drupal/console --with-dependencies