Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Last active November 28, 2019 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmolivas/d7fb74a8036878ad0b0eb5581313a0d9 to your computer and use it in GitHub Desktop.
Save jmolivas/d7fb74a8036878ad0b0eb5581313a0d9 to your computer and use it in GitHub Desktop.
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