jsonapi.services.yml | 2 +- src/StackMiddleware/FormatSetter.php | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/jsonapi.services.yml b/jsonapi.services.yml index c209631..9addb16 100644 --- a/jsonapi.services.yml +++ b/jsonapi.services.yml @@ -128,7 +128,7 @@ services: jsonapi.http_middleware.format_setter: class: Drupal\jsonapi\StackMiddleware\FormatSetter calls: - - [setJsonApiPathPrefix, ['@jsonapi.resource_type.repository']] + - [setJsonApiBasePath, ['@jsonapi.resource_type.repository']] tags: # Set priority to 201 so it happens right before the page cache # middleware (priority 200)has the opportunity to respond. diff --git a/src/StackMiddleware/FormatSetter.php b/src/StackMiddleware/FormatSetter.php index 4072b8d..7befaba 100644 --- a/src/StackMiddleware/FormatSetter.php +++ b/src/StackMiddleware/FormatSetter.php @@ -22,11 +22,11 @@ class FormatSetter implements HttpKernelInterface { protected $httpKernel; /** - * The JSON API path prefix. + * The JSON API base path. * * @var string */ - protected $jsonApiPathPrefix; + protected $jsonApiBasePath; /** * Constructs a FormatSetter object. @@ -39,13 +39,15 @@ class FormatSetter implements HttpKernelInterface { } /** - * Sets the JSON API path prefix. + * Sets the JSON API base path. * * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository * The JSON API resource type repository. */ - public function setJsonApiPathPrefix(ResourceTypeRepositoryInterface $resource_type_repository) { - $this->jsonApiPathPrefix = $resource_type_repository->getPathPrefix(); + public function setJsonApiBasePath(ResourceTypeRepositoryInterface $resource_type_repository) { + $this->jsonApiBasePath = $resource_type_repository->getBasePath(); + assert($this->jsonApiBasePath[0] === '/'); + assert(substr($this->jsonApiBasePath, -1) !== '/'); } /** @@ -79,7 +81,7 @@ class FormatSetter implements HttpKernelInterface { // Check if the path indicates that the request intended to target a JSON // API route (but may not have because of an incorrect parameter or minor // typo). - $jsonapi_route_intended = strpos($request->getPathInfo(), "/{$this->jsonApiPathPrefix}/") !== FALSE; + $jsonapi_route_intended = strpos($request->getPathInfo(), "{$this->jsonApiBasePath}/") !== FALSE; // Check if the 'Accept' header includes the JSON API MIME type. $request_has_jsonapi_media_type = count(array_filter($request->getAcceptableContentTypes(), function ($accept) { return strpos($accept, 'application/vnd.api+json') === 0;