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
- Getting Started (3)
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.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.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.
- Building the Core (5)
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.Triggering a Server Round-Trip
It's JavaScript time! To support features likewire: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.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.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.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.
- Adding More Features (4)
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.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.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.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.
- Wrapping Up (2)
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.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.
- Extra Credit (1)
