drupal - 来自 PHP 文件的 Drupal 8 服务调用
问题描述
我想在外部 php 文件中运行 Drupal 8 的代码。请指导我实现这一目标。
$user = \Drupal::currentUser();
\Drupal::service('session_manager')->destroy();
$user->setAccount(new AnonymousUserSession());
解决方案
当您直接访问此页面时,它将注销活动用户。如果没有活动会话,它什么也不做,只是显示一个空白页面。
坦率地说,除非您必须使用这种方法,否则如果您详细解释您的目标,可能会有更好的“Drupal 方式”解决方案。
否则,此脚本有效:
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
// Change the directory to the Drupal root.
chdir('..');
$autoloader = require_once 'autoload.php';
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->prepareLegacyRequest($request);
\Drupal::moduleHandler()->addModule('user', 'core/modules/user');
\Drupal::moduleHandler()->load('user');
$account = \Drupal::service('authentication')->authenticate($request);
if ($account) {
\Drupal::currentUser()->setAccount($account);
user_logout();
}
推荐阅读
- linux-kernel - 最常见的总线主控操作有哪些,它们比常规 DMA 好多少?
- evernote - 即使已激活,我的 Evernote 令牌也无法用于生产
- azure - Azure 数据工厂 - 源到接收器自定义映射将字符串转换为对象
- php - 如果数据库PHP中不存在行,如何返回零
- swift - 如果帐户尚未在身份验证数据库中,是否有任何方法可以禁用帐户创建?
- python - 即使设置了 buff_size,Ros 订阅者也会处理旧消息
- kibana - Kibana 仪表板:部分消息匹配的聚合
- java - 为什么我不能发送电子邮件?
- javascript - RXJS 的 Subject inside Function
- python - 解压缩 .gz 文件以创建新行分隔的 json 文件 - 创建新行时出现问题