polidog/hypermedia-bundle

dev-master 2020-02-16 11:53 UTC

This package is auto-updated.

Last update: 2024-12-16 22:38:46 UTC


README

HypermediaAPI bundle. This bundle support only HAL json. And you mast need polidog/simple-api-bundle

Installation

$ composer require polidog/hypermedia-bundle "dev-master" 
<?php

return [
...


    Polidog\SimpleApiBundle\PolidogSimpleApiBundle::class => ['all' => true],
    Polidog\HypermediaBundle\HypermediaBundle::class => ['all' => true]
];

Introduce bundle configuration to your config file

# config/packages/polidog_hypermedia.yml

polidog_hypermedia: ~
    hal_content_type: false # default false

If hal_content_type is true that need request header for application/hal+json.

Usage

<?php

declare(strict_types=1);

namespace App\Controller\Api\Projects;

use App\Repository\ProjectRepository;
use Polidog\HypermediaBundle\Annotations\Embed;
use Polidog\HypermediaBundle\Annotations\Link;
use Polidog\SimpleApiBundle\Annotations\Api;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/projects/{code}/detail", requirements={"code"})
 * @Api(statusCode=200)
 *
 * @Embed(rel="members", src="/api/projects/{code}/members")
 *
 * @Link(rel="projects", href="/projects")
 * @Link(rel="project-new", href="/projects/new")
 *
 */
class DetailController
{
    /**
     * @var ProjectRepository
     */
    private $repository;

    public function __construct(ProjectRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * @return array<string,array|null>
     */
    public function __invoke(string $code): array
    {
        $project = $this->repository->findProjectCode($code);

        return [
            'project' => null !== $project ? $project->export() : null,
        ];
    }
}

IMAGE