PHP code example of bunny-poooh / laravel-mercure-publisher

1. Go to this page and download the library: Download bunny-poooh/laravel-mercure-publisher 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/ */

    

bunny-poooh / laravel-mercure-publisher example snippets


return [
    'hub' => [
        'url' => env('MERCURE_PUBLISH_URL','http://127.0.0.1:3000/hub'),
        'jwt' => env('MERCURE_JWT_SECRET'),
    ],
    'jwt_provider' => null,
    'queue_name' => null,
];


namespace App\Http\Controllers;

use Symfony\Component\Mercure\Update;
use Idplus\Mercure\Publify;

class MercureController extends Controller
{
    public function pub(Publify $publisher)
    {
        // ...
        $data = ['status' => 'OutOfStock'];
        $update = new Update(
            'http://topic.iri.local/sub/1',
            json_encode($data)
        );
        $publisher($update);
        // ...

        return view('pub', $data);
    }


namespace App;

use Idplus\Mercure\Jwt\JwtProvider;

class CustomJwt implements JwtProvider
{
    /**
     * Return custom JWT
     *
     * @return string
     */
    public function __invoke(): string
    {
        return 'my.custom.jwt';
    }
}

    ...
    'jwt_provider' => "\App\CustomJwt",



namespace App\Http\Controllers;

class MercureController extends Controller
{
    public function __construct()
    {
        $this->middleware('mercure.discover'); // Auto inject 'Link' header
    }
...
 bash
php artisan vendor:publish --provider="Idplus\Mercure\MercureServiceProvider"