VocĂŞ sabe escrever SPA no Laravel? Em suma, nĂŁo Ă© muito conveniente. Claro, vocĂŞ pode usar qualquer estrutura de front-end. Tradicionalmente, Ă© comum trabalhar com o pacote Laravel + Vue.js.
Escrevemos todo o frontend em Vue.js resources/js
e usamos o Laravel como uma API.
Algo assim:
Vue.js
// resources/js/pages/Users.vue
<template>
<div v-for="user in users" :key="user.id">
<a :href="`/users/${user.id}`">
{{ user.name }}
</a>
<div>{{ user.email }}</div>
</div>
</template>
<script>
export default {
data() {
return {
users: []
}
},
methods: {
async loadUsers() {
const { data } = await this.$axios.get('/api/users');.
this.users = data;
}
},
async beforeMount() {
await this.loadUsers();
}
}
</script>
Laravel
// routes/api.php
Route::get('/users', function index(User $user) {
return $user->all();
});
, AJAX-.
? . . AJAX – . , Blade, , .
Blade, , :
return view('users', [
'users' => $user->all()
]);
? , - -. Blade - . Laravel – API, - . .. ..
Inertia.js. , Blade. – Vue, React Svelte.
Laravel, Inertia.js Rails. . (, Symfony Yii2).
Vue.js Laravel. , .
return Inertia::render('Users', [
'users' => $user->all()
]);
props
.
props: {
users: Array,
},
! .
- №2: (Vue Router, React Router) . – routes
Vue- resources/js/Pages
( ). , Blade, , component.blade.php
Component.vue
.
: . AJAX-, , - . AppServiceProvider
Inertia::share([
'errors' => function () {
return Session::get('errors')
? Session::get('errors')->getBag('default')->getMessages()
: (object) [];
},
]);
Vue- $page.errors
.
<div v-if="$page.errors.first_name">{{ $page.errors.first_name[0] }}</div>
.
, ( ), "", , .
Inertia::share([
//
'app' => [
'name' => Config::get('app.name')
],
//
'auth' => function () {
return [
'user' => Auth::user() ? [
'id' => Auth::user()->id,
'first_name' => Auth::user()->first_name,
'last_name' => Auth::user()->last_name,
] : null
];
}
]);
, , , . - , .
, .
-, Inertia.js . , - Inertia.js, . , ( ).
-, Server-Side Rendering (SSR). , , , , SPA SSR, . , , , , . SSR, – Livewire. - .
-, Inertia.js – . – v0.1.9. , .
, web- .