Bundle to use Laravel Livewire with Symfony

Installs: 1 970

Dependents: 0

Suggesters: 0

Security: 0


v0.6.5 2021-05-11 09:16 UTC

This package is not auto-updated.

Last update: 2022-06-21 17:10:28 UTC


Adaption of the backend part of Laravel Livewire for the Symfony Framework.

⚠ Important: This bundle is currently in the POC (proof of concept) stage. It is in no way recommended for production use!


based on the Livewire quickstart guide.

Install Livewire

Require the symfony-livewire-bundle package.

composer require roflcopter24/symfony-livewire-bundle

Register the Bundle in your Symfony app config/bundles.php:


return [
    Terminal42\ServiceAnnotationBundle\Terminal42ServiceAnnotationBundle::class => ['all' => true],
    RoflCopter24\SymfonyLivewireBundle\SymfonyLivewireBundle::class => ['all' => true],

Register the route in config/routes.yaml:

    resource: '@SymfonyLivewireBundle/Resources/config/routes.yml'

Include the JavaScript in your Twig-Template (on every page that will be using Livewire).

    {{ livewireStyles() }}

    {{ livewireScripts() }}

Symfony Livewire makes use of service discovery for your components.\ First create a source directory for your components: src/Component.

Then make sure it is included in your config/services.yaml and enable the use of the ContainerAwareInterface.\ It should look similar to this:

  # default configuration for services in *this* file
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        - [ setContainer, [ '@service_container' ] ]

  # makes classes in src/ available to be used as services
  # this creates a service per class whose id is the fully-qualified class name
    resource: '../src/'
    public: true
      - '../src/DependencyInjection/'
      - '../src/Entity/'
      - '../src/Kernel.php'
      - '../src/Tests/'

Don't forget to clear your application cache!

Create a component

In the src/Component directory create a new PHP file called Counter.php:

declare(strict_types = 1);

namespace App\Component;

use RoflCopter24\SymfonyLivewireBundle\Annotation\Livewire;
use RoflCopter24\SymfonyLivewireBundle\Component\LivewireComponent;

 * @Livewire("counter")
class CounterComponent extends LivewireComponent
    public int $count = 0;
    public function increment(): void

Note the @Livewire class annotation. This specifies the name of the component.

Now create a Twig template for your component in templates/livewire called counter.html.twig:

<div style="text-align: center">
    <button wire:click="increment">+</button>
    <h1>{{ count }}</h1>

If no method called render is defined in the component class, it will automatically try to use a twig template with the components name from the templates/livewire directory (templates/livewire/<component>.html.twig).

The components public properties are passed to the template by default.

Include the component

There are two ways to include your component:

  1. By directive <wire:component-name />
  2. By Twig function {{ livewire('component-name') }}

Try it:

    {{ livewireStyles() }}
    <wire:counter />


    {{ livewireScripts() }}

View it in the browser

If everything went well, you should now be able to see your component in the browser and clicking the +-Button should increase the counter.