Skip to main content
Version: 2023.3

How to Build a Custom REST API Endpoint

Pimcore offers a bundle called Datahub, offering a highly configurable GraphQL interface on most Pimcore entities.

However a common use case for applications build with Pimcore is integrating with external systems, which requires custom response from API endpoints.

One way to achieve this requirement is to build custom controller action that exposes just the right data in the desired format.

Example

<?php

namespace App\Controller;

use Pimcore\Model\DataObject;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use \Pimcore\Controller\FrontendController;

class CustomRestController extends FrontendController
{
/**
* @Route("/custom-pimcore-webservice/rest/get-products")
*/
public function defaultAction(Request $request): JsonResponse
{
// do some authorization here ...

$blogs = new DataObject\BlogArticle\Listing();

foreach ($blogs as $key => $blog) {
$data[] = array(
"title" => $blog->getTitle(),
"description" => $blog->getText(),
"tags" => $blog->getTags());
}

return $this->json(["success" => true, "data" => $data]);
}
}

Sometimes it is necessary to serialize complete element for API response. This can be achieved by overriding a model which implements the \JsonSerializable interface and implementing jsonSerialize method to return the data you require to be serialized.

<?php

namespace App\Model\DataObject;

class BlogArticle extends \Pimcore\Model\DataObject\BlogArticle implements \JsonSerializable
{
public function jsonSerialize(): array
{
$vars = get_object_vars($this);

return $vars;
}
}