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

Browse all series

Livewire Uncovered

Livewire can often feel like magic. Somehow, you're able to write interactive web applications using only PHP? How? Well, in this series, we're going to uncover the magic together by building a simplified version of Livewire from scratch.

Aside from being plain entertaining, this exercise will arm you with deep Livewire knowledge that will help you build and debug your apps in your daily life. Let's dive in!

Progress

Series Info

Episodes
15
Run Time
2h 53m
Difficulty
Advanced
Last Updated
Jul 7, 2022
Version
Livewire 2

Series Episodes

  1. Getting Started (3)
    1. Series Introduction

      Before we construct our own version of Livewire from scratch, let's first build a basic component using the actual Livewire framework to familiarize ourselves and establish a goal for this series.
    2. Starting From Scratch

      Now that we have a working "counter" component, let's remove the Livewire package from our application powering it, and start building our own version as a replacement. We'll start by getting the initial page to properly render.
    3. Refactoring Our Work

      Before we continue on, let's take what we've written so far, add a few extra niceties, and establish the code structure we're going to use for the rest of the series.
  2. Building the Core (5)
    1. Storing a Snapshot

      To provide the experience of a Livewire component being "live," we need to create and store a snapshot of the component's state in JavaScript, so we can send it back to the server before the next interaction.
    2. Triggering a Server Round-Trip

      It's JavaScript time! To support features like wire:click, we need to listen for page interactions using JavaScript, and trigger a network request to the server containing everything it needs to update our "counter" component.
    3. Handling a Request

      Using the snapshot sent to our backend via JavaScript, we're going to re-create our Livewire component in PHP and call the "increment" method on it. Then we'll send the updated HTML and snapshot back to the browser to update the page.
    4. Complete the Round Trip

      Using the data returned from the backend, we can now complete the round-trip and update our component's HTML on the page using JavaScript.
    5. Intelligently Updating the Page

      In the last episode, we updated the component on the page by replacing its HTML with what was returned from the server. This appears to work fine, but upon further inspection, this strategy is extremely restricting. Instead, we need to intelligently update only the parts of the HTML that need to be changed, rather than wiping it out completely with each request. Let's figure out how.
  3. Adding More Features (4)
    1. Build a To-Do Component

      Our counter component has served us well so far, but to continue building more powerful features in Livewire - like wire:model - we'll need to work with a different example component: a simple to-do list page.
    2. Supporting wire:model

      The wire:model syntax in Livewire is a convenient way to bind data from your component to input elements on the page. It's considered an essential Livewire feature, and therefore, we stand to benefit greatly from understanding how it works at a fundamental level.
    3. Lifecycle Hooks

      Lifecycle hooks like mount and updated are important features of Livewire that allow developers to customize how a component is initialized and later updated. Fortunately, supporting these features is simpler than you might expect.
    4. Supporting Complex Component Properties

      Our simplified version of Livewire only supports setting public properties to data types that are common to both PHP and JavaScript (strings, integers, arrays, etc.). Let's discuss what it might take to support setting something like an Eloquent Model or a Laravel Collection as public property on our component.
  4. Wrapping Up (2)
    1. Hacking Livewire

      What we've built so far, though functional, is actually incredibly insecure. You'll be surprised how easily we can hack into our database using nothing but the "to-do" component that we have on the page. After attacking our application, we'll plug up those security holes and learn about crucial pieces of Livewire security.
    2. Comparison With the Real Livewire

      Now that we have a functioning version of Livewire, let's take a side-by-side look at the similarities and differences between our version and the real one. Now that you've completed this exercise, you'll have a MUCH easier time understanding, inspecting, and contributing to the actual Livewire codebase.
  5. Extra Credit (1)
    1. MorphDom From Scratch

      In Episode 8, we introduced the use of Alpine's "morph" plugin. This black box has served us well, but there is a lot to be gained by more fully understanding how it works. Because building it from scratch was too complicated to tackle during the series, I've added a bonus episode where we can take our time and dive deep into this important function of Livewire.

Continue Learning