Skip to content

Commit 5bb09fa

Browse files
ildyriaqwerty287
andauthored
Quick updates on docs, since Livewire v3 a lot of thing changed 😬 (#97)
* quick updates on docs, since Livewire v3 a lot of thing changed * more details * Update docs/livewire.md Co-authored-by: qwerty287 <[email protected]>
1 parent 70aa1d0 commit 5bb09fa

File tree

1 file changed

+33
-63
lines changed

1 file changed

+33
-63
lines changed

docs/livewire.md

Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
## A brief introduction to Livewire
22

3-
We first give a short intuition of how Livewire, the framework of the new front-end, works.
3+
We first give a short intuition of how [Livewire](https://livewire.laravel.com/docs/quickstart), the framework of the new front-end, works.
44

55
### Livewire and Controllers
66

77
The Laravel framework on which Lychee is based provides two options regarding how a request may be routed:
88

99
- the request is directed to a controller below `app/Http/Controllers`
10-
- the request is directed to a Livewire component below `app/Http/Livewire`
10+
- the request is directed to a Livewire component below `app/Livewire/Components`
1111

1212
In the first case, a controller returns a JSON object or an HTML page rendered from a blade template.
1313
In the second case (Livewire), only a block of HTML built from a blade template will be returned.
1414

1515
From a very high level, Livewire makes use of two folders:
1616

17-
- `app/Http/Livewire` which hosts the components and their inner logic.
18-
- `resources/views/components` which hosts the HTML layout of the components.
17+
- `app/Livewire/Components` which hosts the components and their inner logic.
18+
- `resources/views/{components|livewire}` which hosts the HTML layout of the components.
1919

2020
So far one could say there is not much difference with a normal request life cycle. Where Livewire starts to shine is in the ability to call the methods of the component directly _from HTML_. Instead of having to use a JavaScript hook, make an AJAX call, parse the result, and update the layout accordingly, the user/programmer can directly interact with the designated component and Livewire takes care of the request and rendering.
2121

@@ -37,7 +37,7 @@ Laravel then calls the `render()` method and returns the corresponding view.
3737
```php
3838
public function render()
3939
{
40-
return view('components.molecules.album');
40+
return view('components.gallery.album');
4141
}
4242

4343
```
@@ -57,9 +57,9 @@ One could say that the Livewire components are just Laravel components on steroi
5757

5858
In order to call Livewire, the following syntax is used:
5959
```html
60-
<livewire:sidebar.album :album="$this->album" />
60+
<livewire:pages.gallery.album :album="$this->album" />
6161
```
62-
This will call the Livewire component located at `app/Http/Livewire/Sidebar/Album.php`.
62+
This will call the Livewire component located at `app/Livewire/Components/Pages/Gallery/Album.php`.
6363
As opposed to their Laravel counterparts, it is not possible to use the constructor to read the attributes.
6464
For this reason a method `mount()` has been added:
6565
```php
@@ -72,16 +72,16 @@ Then the `render()` function is used in order to retrieve the respective HTML se
7272
```php
7373
public function render()
7474
{
75-
return view('livewire.sidebar.album');
75+
return view('livewire.pages.gallery.album');
7676
}
7777
```
7878

7979
#### Calling methods
8080

8181
Where Livewire becomes interesting is in its ability to call PHP functions directly from user-land.
8282
Methods of the component can be callable by using additional HTML attributes.
83-
For example in `resources/views/livewire/components/modules/album.php` the attribute `wire:click="login"`
84-
will call the `login()` method of the component `app/Http/Livewire/Header.php`
83+
For example in `resources/views/livewire/pages/gallery/albums.php` the attribute `wire:click="login"`
84+
will call the `login()` method of the component `app/Livewire/Pages/Gallery/Album.php`
8585
```html
8686
<a wire:click="login" class="button">
8787
<x-icons.iconic icon='account-login' />
@@ -100,80 +100,41 @@ This method will then execute and trigger a re-rendering of the component.
100100
In addition to calling a method of a component,
101101
it is also possible to trigger actions in other components.
102102
As a matter of fact, in the specific case of the `login()` method,
103-
we are emitting an event which will trigger the opening of the `app/Http/Livewire/Components/Modal.php` component with a login form:
103+
we are emitting an event which will trigger the opening of the `app/Livewire/Components/Base/Modal.php` component with a login form:
104104
```php
105105
protected function login()
106106
{
107107
$form = 'forms.login';
108108
$params = [];
109-
$this->emitTo('components.modal', 'openModal', $form, $params);
109+
$this->dispatch('openModal', $form, $params)->to(Modal::class);
110110
}
111111
```
112112
`Modal.php` contains the following code:
113113
```php
114114
class Modal extends Openable
115115
{
116-
protected $listeners = [
117-
'openModal',
118-
// ...
119-
];
120-
116+
#[On('openModal')]
121117
public function openModal(string $type, array $params = [])
122118
{
123119
//...
124120
}
125121
```
126-
The protected variable `$listeners` informs the component which events it needs to react to.
127-
In this specific case `openModal()` method has two arguments which are provided by the `$this->emitTo('componentName', 'eventName', $arg1, $arg2);`
122+
The attribute `#[On('event-name')]` informs the component which events it needs to react to.
123+
In this specific case `openModal()` method has two arguments which are provided by the `$this->dispatch('eventName', $arg1, $arg2);`
128124

129125
With these tools at hand, we are now able to understand a bit better the structure of the Livewire front-end.
130126

131127
## Lychee Livewire Front-end
132128

133-
A high-level view is presented in the following picture (at the time of writing, open full image for better view).
134-
135-
<!-- [![](img/Livewire Class Diagram.jpg)](img/Livewire Class Diagram.jpg) -->
136-
[![](img/Livewire-component-diagram.png)](img/Livewire-component-diagram.png)
137-
138-
For now the entry point is always `app/Http/Livewire/Pages/Fullpage.php`.
139-
The path of the code is the following:
140-
141-
1. Calls `app/Http/Livewire/Pages/Fullpage.php`
142-
2. Loads:
143-
- the `Top` level,
144-
- or the requested `Album` and children,
145-
- or the requested `Album` and requested `Photo`
146-
3. Renders `resources/views/livewire/pages/fullpage.blade.php`
147-
4. Calls:
148-
- `app/Http/Livewire/Component/Header.php`
149-
- and one of the `$module`
150-
- `app/Http/Livewire/Modules/Albums.php`
151-
- or `app/Http/Livewire/Modules/Album.php`
152-
and `app/Http/Livewire/Components/Sidebar.php`
153-
- or `app/Http/Livewire/Modules/Photo.php`
154-
and `app/Http/Livewire/Components/Sidebar.php`
155-
5. Renders `resources/views/livewire/components/header.blade.php`
156-
and calls the header layout corresponding to the current module selected:
157-
- `resources/views/livewire/components/modules/albums.php`
158-
- or `resources/views/livewire/components/modules/album.php`
159-
- or `resources/views/livewire/components/modules/photo.php`
160-
6. Renders the module selected; in the case of
161-
`app/Http/Livewire/Modules/Photo.php` we also call the
162-
`app/Http/Livewire/Components/PhotoOverlay.php`
163-
7. Renders the sidebar `app/Http/Livewire/Components/Sidebar.php`
164-
which calls:
165-
- `app/Http/Livewire/Sidebar/Album.php`
166-
- or `app/Http/Livewire/Sidebar/Photo.php`
167-
8. At this point, you pretty much have the full picture...
168-
169-
In summary:
170-
171-
1. Load FullPage
172-
2. Load Header
173-
3. Load Module
174-
4. Load Sidebar
175-
176-
Each step is basically bouncing between the PHP and the blade part.
129+
Since Livewire v3, the front-end uses a layout which is significantly close to a normal website.
130+
We make use of `navigate`. Read more [here](https://livewire.laravel.com/docs/navigate)
131+
132+
Each page is now located in `App/Livewire/Components/Page` with the exception of the different gallery views which are in
133+
`App/Livewire/Components/Page/Gallery`.
134+
135+
This makes use of a unified layout provided in `resources/views/components/layouts/app.blade.php`.
136+
137+
**More info to come.**
177138

178139
## Pitfalls & Wireable
179140

@@ -187,3 +148,12 @@ Do note that by default, the dehydration uses the `toArray()` method. As a resul
187148
For this reason Livewire proposes the interface `Wireable` which requires two methods: `toLivewire()` and `static fromLivewire($value)`.
188149
The former will return the serialized version of the object and the latter will rebuild the object from the serialized version.
189150

151+
Another option is to use Synthesizers. Examples which take care of AbstractAlbums and Photos are in `App/Livewire/Synth`;
152+
153+
## Front-end reactivity
154+
155+
While Livewire provides decent interaction, sometimes we do not need round trips to the server to change the displayed data.
156+
For this reason we use [Alpine.js](https://alpinejs.dev/start-here) which is bundled with Livewire.
157+
158+
Alpine provides convenient tools to improve reactivity such as `x-on`, `x-bind`, `x-data`, `x-show`, `x-init`, `x-cloak`.
159+
We advise the reader to familiarize themselves with those as they are also used in the frontend.

0 commit comments

Comments
 (0)