Laravel Livewire
- 2020年9月21日
- 技術情報
I am talking about the brief introduction about laravel livewire today.
Its aim is to provide a modern and dynamic web experience without having to waste time with reactive web frameworks or Javascript at all.
And for the most part: it can. Livewire really works, and in the next few years we will almost definitely see some big, serious projects on it.
When you activate the livewire, you will
– Write a PHP class, which will act as the backend part of a frontend component. Public fields will be visible to Livewire.
– Write an interface component in the Laravel (Blade) template language. This component refers to the fields in the backend component that you wrote earlier.
– Write methods in your backend component class that change the state of public fields.
– Create an instance of the component in another view or another component.
– When you visit the page, Livewire will render the initial state of the component as HTML and inject it into the page you see. Livewire will have already attached auto-generated JavaScript events to the page, which call the methods you wrote in step 3 above.
– When you take action, the Javascript events that Livewire generates will make an AJAX request to the Livewire endpoint on the backend. This will call the method you wrote, changing the public properties you defined.
-Your backend component’s render method is then called, which produces a snippet of HTML. This HTML snippet is the response to the AJAX request.
– Finally, the client-side Livewire component hot-patches this HTML on the page.
Further more you can look up the livewire lifecycle hooks more details here.
https://laravel-livewire.com/docs/2.x/lifecycle-hooks
Livewire has lots of interesting features but it still have some drawbacks as follow.
– Livewire violates the separation of concerns
– Livewire violates the single-responsibility principle
– Livewire complicates the development workflow
– Too Many AJAX Requests
– Livewire is difficult to test
And Finally maintainability would be harder as the application grows.
yuuma at 2020年09月21日 11:00:43