Jump to another example:
Basics
Store
Composables
Events
Forms
Composables
Composables Showcase: PHP Namespaces
Auto-discovered use* methods with composableState() and callable actions.
How it works
The component uses trait methods named use*.
LiVue auto-discovers them as PHP composables and exposes namespaced data/actions in the template
(for example cart.add(),
cart.increment()).
Cart count (composable)
Server time (meta)
Component id
Composable actions ready
-
(qty: )
- No items yet.
app/LiVue/Composables/UseShowcaseCart.php
public function useCart(): array
{
$state = $this->composableState('cart', ['items' => [], 'nextId' => 1]);
return [
'items' => $state['items'],
'add' => function(string $label) use ($state) { ... },
];
}
resources/views/livue/php-composables-demo.blade.php
<button @click="cart.add(draftLabel)">Add</button>
<button @click="cart.increment(item.id)">+ qty</button>
<button v-click="resetFromPhp">Reset from PHP</button>