Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom JSON Controller
<?php
namespace Drupal\jsonapi_custom\Controller;
use Drupal\Core\Controller\ControllerBase;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\jsonapi_extras\EntityToJsonApi;
class AccountController extends ControllerBase {
/**
* @var \Drupal\jsonapi_extras\EntityToJsonApi
*/
private $entityToJsonApi;
/**
* CustomController constructor.
*
* @param \Drupal\jsonapi_extras\EntityToJsonApi $entity_to_json_api
*/
public function __construct(EntityToJsonApi $entity_to_json_api) {
$this->entityToJsonApi = $entity_to_json_api;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('jsonapi_extras.entity.to_jsonapi')
);
}
/**
* Processes a GET request.
*/
public function me(ServerRequestInterface $request) {
$user = $this->currentUser();
return new JsonResponse(
json_decode($this->entityToJsonApi->serialize($user->getAccount()))
);
}
}
oauth2_token.current_user_info:
path: '/oauth/me'
defaults:
_controller: 'Drupal\jsonapi_custom\Controller\AccountController::me'
methods: [GET]
requirements:
_access: 'TRUE'
_format: 'json'
options:
_auth: ['oauth2']
no_cache: TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment