未找到 Drupal 8 Class 'Drupal\Core\Session\AccountInterface'
Drupal 8 Class 'Drupal\Core\Session\AccountInterface' not found
我正在尝试在我的 Drupal 站点根目录中编写一个自定义 php 脚本来检查用户是否已登录。为了检查这一点,我导入了 bootstrap.inc。但是,当我这样做时,它会抛出这个错误
这是我站点根目录中 php 脚本的代码:
<?php
require_once './core/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
var_dump($user->uid);
?>
有人对此有解决方案吗?
对于bootstrapDrupal 8,你需要不同的代码。 Drupal 8 没有任何 drupal_bootstrap()
函数,因此您使用的代码会抛出 PHP 错误。
您可以使用 authorize.php 作为指导来编写您自己的脚本。
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$autoloader = (require_once 'autoload.php');
try {
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->prepareLegacyRequest($request);
} catch (HttpExceptionInterface $e) {
$response = new Response('', $e->getStatusCode());
$response
->prepare($request)
->send();
exit;
}
\Drupal::moduleHandler()
->addModule('system', 'core/modules/system');
\Drupal::moduleHandler()
->addModule('user', 'core/modules/user');
\Drupal::moduleHandler()
->load('system');
\Drupal::moduleHandler()
->load('user');
$account = \Drupal::service('authentication')
->authenticate($request);
if ($account) {
\Drupal::currentUser()
->setAccount($account);
if (\Drupal::currentUser()->isAuthenticated() {
// The user is logged-in.
}
}
我使用完全不同的方法解决了这个问题。我编写了一个模块,在用户登录 drupal 时设置一个 cookie(我为此使用 hook_user_login)。当用户注销时,我删除该 cookie(我为此使用 hook_user_logout)。这是我的代码 test.module:
/**
* @param $account
*/
function intersoc_content_user_login($account)
{
setcookie("user", "loggedin", time() + (86400 * 30),"/");
}
/**
* @param $account
*/
function intersoc_content_user_logout($account)
{
if (isset($_COOKIE['user']))
{
unset($_COOKIE['user']);
setcookie('user', '', time() - 3600, '/'); //Clearing cookie
}
}
然后在站点根目录中的自定义脚本中检查是否设置了 cookie。当 cookie 存在时 => 用户已登录。如果 cookie 不存在则用户未登录。下面的 isLoggedIn() 函数:
/**
* @return bool which indicates if the user is logged in or not
*/
private function isLoggedIn()
{
if(isset($_COOKIE["user"]))
{
return TRUE;
}
else
{
return FALSE;
}
}
这不是最漂亮的解决方案,但它确实有效!!!
我正在尝试在我的 Drupal 站点根目录中编写一个自定义 php 脚本来检查用户是否已登录。为了检查这一点,我导入了 bootstrap.inc。但是,当我这样做时,它会抛出这个错误
这是我站点根目录中 php 脚本的代码:
<?php
require_once './core/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
var_dump($user->uid);
?>
有人对此有解决方案吗?
对于bootstrapDrupal 8,你需要不同的代码。 Drupal 8 没有任何 drupal_bootstrap()
函数,因此您使用的代码会抛出 PHP 错误。
您可以使用 authorize.php 作为指导来编写您自己的脚本。
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$autoloader = (require_once 'autoload.php');
try {
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->prepareLegacyRequest($request);
} catch (HttpExceptionInterface $e) {
$response = new Response('', $e->getStatusCode());
$response
->prepare($request)
->send();
exit;
}
\Drupal::moduleHandler()
->addModule('system', 'core/modules/system');
\Drupal::moduleHandler()
->addModule('user', 'core/modules/user');
\Drupal::moduleHandler()
->load('system');
\Drupal::moduleHandler()
->load('user');
$account = \Drupal::service('authentication')
->authenticate($request);
if ($account) {
\Drupal::currentUser()
->setAccount($account);
if (\Drupal::currentUser()->isAuthenticated() {
// The user is logged-in.
}
}
我使用完全不同的方法解决了这个问题。我编写了一个模块,在用户登录 drupal 时设置一个 cookie(我为此使用 hook_user_login)。当用户注销时,我删除该 cookie(我为此使用 hook_user_logout)。这是我的代码 test.module:
/**
* @param $account
*/
function intersoc_content_user_login($account)
{
setcookie("user", "loggedin", time() + (86400 * 30),"/");
}
/**
* @param $account
*/
function intersoc_content_user_logout($account)
{
if (isset($_COOKIE['user']))
{
unset($_COOKIE['user']);
setcookie('user', '', time() - 3600, '/'); //Clearing cookie
}
}
然后在站点根目录中的自定义脚本中检查是否设置了 cookie。当 cookie 存在时 => 用户已登录。如果 cookie 不存在则用户未登录。下面的 isLoggedIn() 函数:
/**
* @return bool which indicates if the user is logged in or not
*/
private function isLoggedIn()
{
if(isset($_COOKIE["user"]))
{
return TRUE;
}
else
{
return FALSE;
}
}
这不是最漂亮的解决方案,但它确实有效!!!