UserLogout.php in Services 9.0.x
File
src/Plugin/ServiceDefinition/UserLogout.php
View source
<?php
namespace Drupal\services\Plugin\ServiceDefinition;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\services\ServiceDefinitionBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Symfony\Component\Serializer\SerializerInterface;
class UserLogout extends ServiceDefinitionBase {
use MessengerTrait;
public function processRoute(Route $route) {
$route
->setRequirement('_user_is_logged_in', 'TRUE');
}
public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
user_logout();
$this
->messenger()
->addMessage(t('User successfully logged out'), 'status', FALSE);
return [];
}
}
Classes
Name |
Description |
UserLogout |
Plugin annotation
@ServiceDefinition(
id = "user_logout",
methods = {
"POST"
},
title = @Translation("User logout"),
description = @Translation("Allows users to logout."),
category = @Translation("User"),
path = "user/logout"
) |