Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GdS's avatar
Level 26

Request: Laravel project using Api Platform, Frankenphp, Mercure

Since I can foresee the impact of these technologies I am trying to wrap my head around them, but I am struggling with the available Laravel flavoured docs.

I have also binge-watched videos from Chris Fidao and Just Steve King, but Mercure is still missing.

@laracasts @jeffreyway Could it be interesting?

This may be a somewhat advanced topic, yet manageable after watching the excellent Docker, Building Apis and Octane lessons. I could transfer lots of knowledge from them.

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! Here’s a high-level guide and some code snippets to help you integrate API Platform, FrankenPHP, Mercure, and Laravel. This is an advanced setup, so familiarity with Docker, API design, and real-time communication is assumed.


1. Project Structure

You’ll likely want to separate your API Platform (Symfony) and Laravel projects, but they can communicate via HTTP or events (e.g., Mercure). Here’s a suggested structure:

/api-platform   # Symfony + API Platform + Mercure
/laravel        # Laravel app
/docker         # Docker configs for FrankenPHP, Mercure, etc.

2. API Platform with Mercure and FrankenPHP

  • API Platform natively supports Mercure for real-time updates.
  • FrankenPHP can serve your Symfony app and handle HTTP/2, async, etc.

docker-compose.yml (simplified):

version: '3.8'
services:
  api:
    image: dunglas/frankenphp
    volumes:
      - ./api-platform:/app
    environment:
      MERCURE_PUBLISHER_JWT_KEY: '!ChangeMe!'
      MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeMe!'
    ports:
      - "8080:80"
    depends_on:
      - mercure

  mercure:
    image: dunglas/mercure
    environment:
      JWT_KEY: '!ChangeMe!'
      ALLOW_ANONYMOUS: 1
      CORS_ALLOWED_ORIGINS: '*'
    ports:
      - "3000:80"

api-platform/.env:

MERCURE_URL=http://mercure/.well-known/mercure
MERCURE_PUBLIC_URL=http://localhost:3000/.well-known/mercure
MERCURE_JWT_SECRET=!ChangeMe!

config/packages/api_platform.yaml:

api_platform:
    mercure: true

3. Laravel Integration

You have two main options:

Option 1: Laravel as a Consumer

Laravel can subscribe to Mercure updates (e.g., via JavaScript or PHP client) or consume the API Platform endpoints.

Example: Subscribing to Mercure in Laravel (JS frontend):

const url = new URL('http://localhost:3000/.well-known/mercure');
url.searchParams.append('topic', 'https://example.com/books/{id}');

const eventSource = new EventSource(url);

eventSource.onmessage = event => {
    console.log(JSON.parse(event.data));
};

Option 2: Laravel as a Publisher

Laravel can publish updates to Mercure using HTTP POST with JWT authentication.

Example: Publishing from Laravel

Install Guzzle:

composer require guzzlehttp/guzzle

Laravel code:

use GuzzleHttp\Client;
use Firebase\JWT\JWT;

$jwt = JWT::encode(['mercure' => ['publish' => ['*']]], '!ChangeMe!', 'HS256');

$client = new Client();
$response = $client->post('http://localhost:3000/.well-known/mercure', [
    'headers' => [
        'Authorization' => 'Bearer ' . $jwt,
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'form_params' => [
        'topic' => 'https://example.com/books/1',
        'data' => json_encode(['status' => 'updated']),
    ],
]);

4. Tips

  • Use API Platform for your API and real-time updates (Mercure).
  • Use Laravel for your main app logic, consuming the API or subscribing to Mercure.
  • Use FrankenPHP to serve your Symfony app for performance and async capabilities.
  • Use Docker Compose to orchestrate everything.

5. Further Reading


Summary:

  • Use Docker Compose to run FrankenPHP (serving API Platform), Mercure, and Laravel.
  • API Platform can publish real-time updates to Mercure.
  • Laravel can consume these updates or publish its own.
  • Use JWT for Mercure authentication.

Let me know if you need a more detailed example or a specific integration scenario!

Please or to participate in this conversation.