Headless Symfony App with PHP SDK

This recipe connects a headless Symfony 8 web application to PhrameCMS using phramecms/php-sdk (PhrameCmsBundle), caches published content with a short TTL using Symfony\Contracts\Cache\CacheInterface, renders index and detail pages with Twig, and unit-tests the repository with FakePhrameClient (zero HTTP calls).


Step 1: Mint a Read-Only Agent Token on PhrameCMS

On your PhrameCMS instance, issue a least-privilege Bearer token that can only read content/article entities (zero write or admin permissions):

bash
./bin/phrame op:run agent_token.create \
  --input='{"name":"symfony-frontend","scopes":["entity.content.article.read"]}' \
  --scopes=agent_token.write --json

Store the returned plaintext_token securely in your Symfony app's environment variables.


Step 2: Install & Configure PhrameCmsBundle

Install phramecms/php-sdk in your Symfony 8 application (see the PHP SDK Guide for full installation details):

bash
composer require phramecms/php-sdk symfony/http-client symfony/cache symfony/twig-bundle

Register PhrameCmsBundle in config/bundles.php:

php
<?php

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    PhrameCms\Sdk\Symfony\PhrameCmsBundle::class => ['all' => true],
];

Create config/packages/phrame_cms.yaml:

yaml
phrame_cms:
    base_uri: '%env(PHRAME_API_BASE_URL)%'
    token: '%env(PHRAME_API_TOKEN)%'
    user_agent: 'acme-web/1.0'

And configure .env.local:

dotenv
PHRAME_API_BASE_URL=https://cms.example.test
PHRAME_API_TOKEN=<agent-token>

Step 3: Build a TTL-Cached Repository

Create a readonly view DTO (src/Content/Article.php) and a cached repository (src/Content/ArticleRepository.php) that autowires PhrameClientInterface and CacheInterface. Using iterateAll() ensures you walk every page seamlessly even when your catalog grows past a single JSON:API page:

php
<?php

declare(strict_types=1);

namespace App\Content;

use PhrameCms\Sdk\Model\ResourceObject;

final readonly class Article
{
    public function __construct(
        public string $id,
        public string $slug,
        public string $title,
        public string $summary,
        public string $body,
        public ?string $updatedAt,
    ) {}

    public static function fromResource(ResourceObject $resource): self
    {
        return new self(
            id: $resource->id,
            slug: $resource->string('slug'),
            title: $resource->string('title'),
            summary: $resource->nullableString('summary') ?? '',
            body: $resource->nullableString('body') ?? '',
            updatedAt: $resource->updatedAt(),
        );
    }
}
php
<?php

declare(strict_types=1);

namespace App\Content;

use PhrameCms\Sdk\Contract\PhrameClientInterface;
use PhrameCms\Sdk\Query\ContentQuery;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

final readonly class ArticleRepository
{
    private const int CACHE_TTL_SECONDS = 300;

    public function __construct(
        private PhrameClientInterface $phrame,
        private CacheInterface $cache,
    ) {}

    /** @return list<Article> */
    public function allPublished(): array
    {
        return $this->cache->get('phrame.articles.published', function (ItemInterface $item): array {
            $item->expiresAfter(self::CACHE_TTL_SECONDS);

            $query = ContentQuery::create()
                ->filter('state', 'published')
                ->sort('-createdAt')
                ->limit(100);

            $articles = [];
            foreach ($this->phrame->content()->iterateAll('content', 'article', $query) as $resource) {
                $articles[] = Article::fromResource($resource);
            }

            return $articles;
        });
    }

    public function findPublishedBySlug(string $slug): ?Article
    {
        $cacheKey = 'phrame.article.slug.' . hash('sha256', $slug);

        return $this->cache->get($cacheKey, function (ItemInterface $item) use ($slug): ?Article {
            $item->expiresAfter(self::CACHE_TTL_SECONDS);

            $resource = $this->phrame->content()->findOneBy('content', 'article', [
                'slug' => $slug,
                'state' => 'published',
            ]);

            return $resource !== null ? Article::fromResource($resource) : null;
        });
    }
}

Step 4: Render with a Symfony Controller & Twig

Create src/Controller/BlogController.php:

php
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Content\ArticleRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class BlogController extends AbstractController
{
    public function __construct(
        private readonly ArticleRepository $articles,
    ) {}

    #[Route('/blog', name: 'blog_index', methods: ['GET'])]
    public function index(): Response
    {
        return $this->render('blog/index.html.twig', [
            'articles' => $this->articles->allPublished(),
        ]);
    }

    #[Route('/blog/{slug}', name: 'blog_show', requirements: ['slug' => '[a-z0-9-]+'], methods: ['GET'])]
    public function show(string $slug): Response
    {
        $article = $this->articles->findPublishedBySlug($slug);
        if ($article === null) {
            throw $this->createNotFoundException(sprintf('Article "%s" not found.', $slug));
        }

        return $this->render('blog/show.html.twig', [
            'article' => $article,
        ]);
    }
}

Create templates/blog/index.html.twig:

twig
{% extends 'base.html.twig' %}

{% block title %}Blog{% endblock %}

{% block body %}
    <h1>Blog</h1>
    <ul>
        {% for article in articles %}
            <li>
                <a href="{{ path('blog_show', { slug: article.slug }) }}">{{ article.title }}</a>
                {% if article.summary != '' %}<p>{{ article.summary }}</p>{% endif %}
            </li>
        {% else %}
            <li>No published articles yet.</li>
        {% endfor %}
    </ul>
{% endblock %}

Step 5: Unit-Test with FakePhrameClient

Because ArticleRepository depends on PhrameClientInterface, you can test pagination, filtering, DTO mapping, and caching completely in-memory with FakePhrameClient and Symfony's ArrayAdapter:

php
<?php

declare(strict_types=1);

namespace App\Tests\Content;

use App\Content\ArticleRepository;
use PhrameCms\Sdk\Model\ResourceObject;
use PhrameCms\Sdk\Testing\FakePhrameClient;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

final class ArticleRepositoryTest extends TestCase
{
    public function testReturnsOnlyPublishedArticlesAndCachesResult(): void
    {
        $fake = (new FakePhrameClient())->withResources(
            new ResourceObject(
                type: 'content--article',
                id: 'ent_1',
                attributes: [
                    'slug' => 'launch-day',
                    'title' => 'Launch Day',
                    'summary' => 'PhrameCMS is live.',
                    'body' => 'Full post body.',
                    'state' => 'published',
                    'createdAt' => '2026-05-02T09:00:00Z',
                ],
            ),
            new ResourceObject(
                type: 'content--article',
                id: 'ent_2',
                attributes: [
                    'slug' => 'internal-draft',
                    'title' => 'Internal Draft',
                    'state' => 'draft',
                    'createdAt' => '2026-05-03T09:00:00Z',
                ],
            ),
        );

        $repository = new ArticleRepository($fake, new ArrayAdapter());

        $articles = $repository->allPublished();
        self::assertCount(1, $articles);
        self::assertSame('launch-day', $articles[0]->slug);

        $found = $repository->findPublishedBySlug('launch-day');
        self::assertNotNull($found);
        self::assertSame('Launch Day', $found->title);

        // Drafts are invisible to findPublishedBySlug
        self::assertNull($repository->findPublishedBySlug('internal-draft'));
    }
}
  • PHP SDK Guide — Full walkthrough of installation, query building, config/schema clients, and error handling.
  • PHP SDK Reference — Authoritative tables for PhrameCmsBundle, ContentQuery, DTOs, and FakePhrameClient.
  • Getting Started — Setting up a local PhrameCMS backend.