Last active
November 28, 2019 23:07
-
-
Save jmolivas/d7fb74a8036878ad0b0eb5581313a0d9 to your computer and use it in GitHub Desktop.
Custom JSON Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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())) | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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