Share
Embed
Livewire. It's like Vue or React. Without the JavaScript.
Christian Leo-Pernold
September 19, 2019
Transcript
43 slides
-
About me ! Christian Leo-Pernold "#$%&'(⛰*+,- . @mazedlx ⭐ github.com/mazedlx
0 mazedlx.net 1 repod.at 3/26 -
Livewire is a full-stack framework for Laravel that makes building
dynamic front-ends as simple as writing vanilla PHP (literally). — Caleb Porzio (@calebporzio) 5/26 -
Example Vue component <template> <div> <button @click="increment">+</button> <button @click="decrement">-</button> <span>{{
count }}</span> </div> </template> <script> export default { data() { return { count: 0 } }, methods: { increment() { this.count++; }, decrement() { this.count--; } } }; </script> 6/26 -
Example Vue component <template> <div> <button @click="increment">+</button> <button @click="decrement">-</button> <span>{{
count }}</span> </div> </template> <script> export default { data() { return { count: 0 } }, methods: { increment() { this.count++; }, decrement() { this.count--; } } }; </script> 6/26 -
Example Vue component <template> <div> <button @click="increment">+</button> <button @click="decrement">-</button> <span>{{
count }}</span> </div> </template> <script> export default { data() { return { count: 0 } }, methods: { increment() { this.count++; }, decrement() { this.count--; } } }; </script> 6/26 -
Example Vue component <template> <div> <button @click="increment">+</button> <button @click="decrement">-</button> <span>{{
count }}</span> </div> </template> <script> export default { data() { return { count: 0 } }, methods: { increment() { this.count++; }, decrement() { this.count--; } } }; </script> 6/26 -
Example Vue component <template> <div> <button @click="increment">+</button> <button @click="decrement">-</button> <span>{{
count }}</span> </div> </template> <script> export default { data() { return { count: 0 } }, methods: { increment() { this.count++; }, decrement() { this.count--; } } }; </script> 6/26 -
Example React component class Counter extends React.Component { constructor(props) {
super(props); this.state = { count: 0, }; } increment() { this.setState({ count: this.state.count + 1 }); } decrement() { this.setState({ count: this.state.count - 1 }); } render() { return ( <div> <button onClick={() => this.increment()}>+</button> <button onClick={() => this.decrement()}>-</button> <span>{this.state.count}</span> </div> ); } } 7/26 -
Example React component class Counter extends React.Component { constructor(props) {
super(props); this.state = { count: 0, }; } increment() { this.setState({ count: this.state.count + 1 }); } decrement() { this.setState({ count: this.state.count - 1 }); } render() { return ( <div> <button onClick={() => this.increment()}>+</button> <button onClick={() => this.decrement()}>-</button> <span>{this.state.count}</span> </div> ); } } 7/26 -
Example React component class Counter extends React.Component { constructor(props) {
super(props); this.state = { count: 0, }; } increment() { this.setState({ count: this.state.count + 1 }); } decrement() { this.setState({ count: this.state.count - 1 }); } render() { return ( <div> <button onClick={() => this.increment()}>+</button> <button onClick={() => this.decrement()}>-</button> <span>{this.state.count}</span> </div> ); } } 7/26 -
Example React component class Counter extends React.Component { constructor(props) {
super(props); this.state = { count: 0, }; } increment() { this.setState({ count: this.state.count + 1 }); } decrement() { this.setState({ count: this.state.count - 1 }); } render() { return ( <div> <button onClick={() => this.increment()}>+</button> <button onClick={() => this.decrement()}>-</button> <span>{this.state.count}</span> </div> ); } } 7/26 -
Example React component class Counter extends React.Component { constructor(props) {
super(props); this.state = { count: 0, }; } increment() { this.setState({ count: this.state.count + 1 }); } decrement() { this.setState({ count: this.state.count - 1 }); } render() { return ( <div> <button onClick={() => this.increment()}>+</button> <button onClick={() => this.decrement()}>-</button> <span>{this.state.count}</span> </div> ); } } 7/26 -
Install via composer $ composer require calebporzio/livewire Using version ^0.2.7
for calebporzio/livewire ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing calebporzio/livewire (v0.2.7): Downloading (100%) Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: beyondcode/laravel-dump-server Discovered Package: calebporzio/livewire Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. 9/26 -
Create a component $ php artisan make:livewire counter ! Files
created: -> [.../app/Http/Livewire/Counter.php] -> [.../resources/views/livewire/counter.blade.php] 11/26 -
The Livewire component namespace App\Http\Livewire; use Livewire\Component; class Counter extends
Component { public function render() { return view('livewire.counter'); } } 12/26 -
The Livewire view <div> {{-- The whole world belongs to
you --}} </div> 13/26 -
Let's add some functionality class Counter extends Component { public
$count = 0; public function increment() { $this->count++; } public function decrement() { $this->count--; } public function render() { return view('livewire.counter'); } } 15/26 -
Let's add some functionality class Counter extends Component { public
$count = 0; public function increment() { $this->count++; } public function decrement() { $this->count--; } public function render() { return view('livewire.counter'); } } 15/26 -
Let's add some functionality class Counter extends Component { public
$count = 0; public function increment() { $this->count++; } public function decrement() { $this->count--; } public function render() { return view('livewire.counter'); } } 15/26 -
Let's add some functionality class Counter extends Component { public
$count = 0; public function increment() { $this->count++; } public function decrement() { $this->count--; } public function render() { return view('livewire.counter'); } } 15/26 -
Let's add some functionality class Counter extends Component { public
$count = 0; public function increment() { $this->count++; } public function decrement() { $this->count--; } public function render() { return view('livewire.counter'); } } 15/26 -
Include the component <html> <head> <title>Livewire Demo</title> </head> <body> <div>
@livewire('counter') </div> @livewireAssets </body> </html> 16/26 -
Include the component <html> <head> <title>Livewire Demo</title> </head> <body> <div>
@livewire('counter') </div> @livewireAssets </body> </html> 16/26 -
1. Livewire hears the click because of wire:click="increment" 2. Livewire
sends an ajax request to PHP 3. PHP calls the corresponding method, which updates $count 4. PHP re-renders the Blade template and sends back the HTML 5. Livewire receives the response, and updates the DOM 19/26 -
Should you replace all your Vue components with Livewire components
now? Not exactly. [...] it's better to use JavaScript for things that need to be instant (like animations) [...] A good rule of thumb is: any JavaScript components that rely on ajax for server communication, will be better off as Livewire components. — Caleb Porzio 22/26 -
Can you use it in your projects? Right now, the
project is still in pre-release. Sign up for my email newsletter to get notified when It's ready for prime-time. — Caleb Porzio 23/26 -
Livewire Fiddle I'm really taking my time with this to
make sure it's beautiful, functional, secure, and user friendly. Honestly, it has some features I wish CodePen had. — Caleb Porzio 24/26