
Drupal 8/9 开发起步
开发环境
Composer 安装 9
# 安装最新 Drupal 的官方指引
composer create-project drupal/recommended-project drupal-composer
composer update "drupal/core-*" --with-dependencies
# Drush 集成版
composer create-project drupal-composer/drupal-project:9.x-dev --no-interaction drupal-composer-9.x-dev
composer update "drupal/*" --with-dependencies
# Console 需要 7.4,最新的 Drupal 需要 8.1,放弃吧!
# 安装 Drupal Console
composer require drupal/console --prefer-dist --optimize-autoloader
Composer 安装 Drupal 8 环境
# 安装 PHP 包管理工具
brew install composer
# 使用 Composer 国内镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
# 安装 Drupal 环境到当前目录的 docs 目录下
composer create-project drupal-composer/drupal-project:8.x-dev --stability dev --no-interaction docs
# 安装 Drupal 9 到当前目录的 docs 目录下
composer create-project drupal/recommended-project docs
通过 Composer 安装的 Drupal 8 自带新版 Drush 支持,全局安装 Drush 8.x 版本后,在安装目录下,运行 Drush 命令时会自动调用 9.x 版本。
通过 Composer 安装的 Drupal 8 还自带 Drupal Console 支持,参考以下方法为 Drupal Console 添加全局支持:
# 全局安装 drupal 命令
php -r "readfile('https://drupalconsole.com/installer');" > /usr/local/bin/drupal
chmod +x /usr/local/bin/drupal
Composer 安装常用 Drupal 8 扩展:
# Themes
composer require 'drupal/drupal8_zymphonies_theme:^1.3'
composer require --dev 'drupal/devel:^1.2'
Drush 安装 Drupal 环境
访问 Drush 主页,安装 Drush 命令行 8.14+ 版本到 /usr/local/bin 目录。Drush 安装的 Drupal 8 仍可使用 Drush 升级。
# 添加最新 Drush 支持
drush dl drupal
升级 Drupal 环境
使用 Composer 安装的 Drupal 使用以下命令升级:
# 检查补丁
composer outdated drupal/*
# 升级 Drupal 8
composer update drupal/core webflo/drupal-core-require-dev --with-dependencies
composer update drupal/* --with-dependencies
# 升级 Drupal 9
composer show drupal/*
composer update drupal/core-recommended --with-dependencies
composer update drupal/core --with-dependencies
## 然后在相关站点目录下运行
drush updatedb / drush updb
drush cache:rebuild
Drush 安装的 Drupal 环境同样使用 Drush 部署升级布丁。
常用设置
Drupal 8 初始化
Drupal 环境安装后需要访问 install.php 去初始化 Drupal 8 站点,也可以用如下命令完成:
# drupal 命令初始化 Drupal 8 环境
drupal site:install standard --langcode en --db-type mysql --db-port 3306 --db-user root --db-pass root --db-host 127.0.0.1
开发环境设置
开发环境的设置保存在 setttings.local.php 文件中,覆盖 settings.php 中默认或生产环境的设置:
<?php
/**
* DB Configurations.
*
* Below configuration is referring MAMP MySQL server.
*/
$databases['default']['default'] = array (
'database' => '<DB Name>',
'username' => 'root',
'password' => 'root',
'prefix' => '',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
/**
* Export configurations to a non web directory.
*/
$config_directories['sync'] = '../config/sync';
/**
* Enable local development services.
*/
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
/**
* Disable CSS and JS aggregation.
*/
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
/**
* Site Information.
*/
// Site Name - Basing on core/modules/system/config/schema/system.schema.yml.
$config['system.site']['name'] = 'Composer (Dev)';
/**
* Suppress status warning.
*/
$settings['trusted_host_patterns'] = [
'.*',
];
/**
* Google Analytics Overrides.
*/
$config['google_analytics.settings']['account'] = "UA-99999999-99";
常用命令
Drush
# 配置管理
drush cex # 导出配置 Drush ^8.14
drush config:export # 导出配置 Drush 9+
# 对于 Drupal 8.6+ 的版本,导出的配置部署到不同的环境后,访问 /admin/config/development/configuration 即可确认并导入配置
常见错误及修复方法
非 drupal 目录执行问题
PHP Fatal error: Uncaught Error: Class "Symfony\Component\HttpKernel\Kernel" not found in ~/.composer/vendor/drush/drush/src/Preflight/Preflight.php:209
Stack trace:
#0 ~/.composer/vendor/drush/drush/src/Runtime/Runtime.php(84): Drush\Preflight\Preflight->loadSymfonyCompatabilityAutoloader()
#1 ~/.composer/vendor/drush/drush/src/Runtime/Runtime.php(51): Drush\Runtime\Runtime->doRun(Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#2 ~/.composer/vendor/drush/drush/drush.php(77): Drush\Runtime\Runtime->run(Array)
#3 ~/.composer/vendor/drush/drush/drush(4): require('~/....')
#4 ~/.composer/vendor/bin/drush(120): include('~/....')
#5 {main}
thrown in ~/.composer/vendor/drush/drush/src/Preflight/Preflight.php on line 209
composer global require symfony/http-kernel
psr/log 版本问题
PHP Fatal error: Declaration of Drush\Application::setLogger(Psr\Log\LoggerInterface $logger) must be compatible with Psr\Log\LoggerAwareInterface::setLogger(Psr\Log\LoggerInterface $logger): void in /vendor/psr/log/Psr/Log/LoggerAwareTrait.php on line 22
# ~/.composer/composer.json
{
"require": {
"laravel/installer": "^4.2",
"psr/log": "^1",
"drush/drush": "^11.3",
"symfony/http-kernel": "^6.1"
},
"config": {
"allow-plugins": {
"composer/installers": true,
"drupal/console-extend-plugin": true
}
}
}
Console
常见问题
Doctrine Annotation Type Error
Composer 在 PHP 7.2 环境下安装的网站代码,在 PHP 7.0 的环境下会出现 Doctrine Annotation 相关 Exception 问题。
Additional uncaught exception thrown while handling exception.
Original
TypeError: Return value of Doctrine\Common\Annotations\AnnotationRegistry::reset() must be an instance of Doctrine\Common\Annotations\void, none returned in Doctrine\Common\Annotations\AnnotationRegistry::reset() (line 55 of /Users/moha/Documents/content/lightning/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php).
Doctrine\Common\Annotations\AnnotationRegistry::reset() (Line: 113)
Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery->getDefinitions() (Line: 86)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 109)
Drupal\Core\Render\ElementInfoManager->buildInfo('seven') (Line: 76)
Drupal\Core\Render\ElementInfoManager->getInfo('form') (Line: 806)
Drupal\Core\Form\FormBuilder->prepareForm('install_select_language_form', Array, Object) (Line: 272)
Drupal\Core\Form\FormBuilder->buildForm('install_select_language_form', Object) (Line: 905)
install_get_form('Drupal\Core\Installer\Form\SelectLanguageForm', Array) (Line: 1330)
install_select_language(Array) (Line: 677)
install_run_task(Array, Array) (Line: 555)
install_run_tasks(Array) (Line: 117)
install_drupal(Object) (Line: 44)
Additional
TypeError: Return value of Doctrine\Common\Annotations\AnnotationRegistry::reset() must be an instance of Doctrine\Common\Annotations\void, none returned in Doctrine\Common\Annotations\AnnotationRegistry::reset() (line 55 of /Users/moha/Documents/content/lightning/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php).
Doctrine\Common\Annotations\AnnotationRegistry::reset() (Line: 113)
Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery->getDefinitions() (Line: 86)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 109)
Drupal\Core\Render\ElementInfoManager->buildInfo('seven') (Line: 76)
Drupal\Core\Render\ElementInfoManager->getInfo('html') (Line: 298)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 139)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 140)
Drupal\Core\Render\Renderer->renderRoot(Array) (Line: 66)
Drupal\Core\Render\BareHtmlPageRenderer->renderBarePage(Array, 'Error', 'install_page', Array) (Line: 76)
Drupal\Core\ProxyClass\Render\BareHtmlPageRenderer->renderBarePage(Array, 'Error', 'install_page', Array) (Line: 1011)
install_display_output(Array, Array, Array) (Line: 260)
_drupal_log_error(Array, 1) (Line: 600)
_drupal_exception_handler(Object)
执行 composer -vvv about,确保运行 Composer 的 PHP 的版本和网站运行的 PHP 版本一致,即 php-cli 和 php-fmp 的版本需要一致可以解决这类问题。Mac OS 下,用下面的命令选择 PHP 版本来安装。
brew install php@7.0
brew link --overwrite php@7.0 --force # Update link under /usr/local/bin & /usr/local/sbin
https://drupal.stackexchange.com/questions/249717/installation-error-with-composer