1. Go to this page and download the library: Download paknahad/jsonapi-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
paknahad / jsonapi-bundle example snippets
use Doctrine\ORM\Mapping as ORM;
class Book
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=20, nullable=true)
*/
private $isbn;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Author", inversedBy="books")
*/
private $authors;
...
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(min=3)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Book", mappedBy="authors")
*/
private $books;
...
use Symfony\Component\Routing\Annotation\Route;
class ProjectController extends Controller
{
/**
* @Route("/", name="projects_index", methods="GET")
*/
public function index(ProjectRepository $projectRepository, ResourceCollection $resourceCollection): ResponseInterface
{
$resourceCollection->setRepository($projectRepository);
$resourceCollection->getQuery()->where('r.user_id = :s1')->setParameter(...);
$resourceCollection->handleIndexRequest();
return $this->jsonApi()->respond()->ok(
new ProjectsDocument(new ProjectResourceTransformer()),
$resourceCollection
);
}
use Paknahad\JsonApiBundle\Helper\Filter\FinderSupportsInterface;
use Paknahad\JsonApiBundle\Helper\FieldManager;
use Symfony\Component\HttpFoundation\Request;
class CustomFinder implements FinderSupportsInterface
{
public function supports(Request $request, FieldManager $fieldManager): bool
{
// based on some request data
if ($request->query->has('some-flag')) {
return true;
}
// based on document field manager
if ($fieldManager->getRootEntity() === Author::class) {
return true;
}
return false;
}
}