Öncelikle Laravel projesini oluştur:
bash
composer create-project laravel/laravel inertia-app
cd inertia-app
Ardından Inertia.js ve gerekli paketleri yükle
bash
composer require inertiajs/inertia-laravel
Vue.js kullanıyorsan
bash
npm install @inertiajs/vue3
React kullanıyorsan
bash
npm install @inertiajs/react
Son olarak, Inertia için middleware'i Laravel'e ekle
bash
php artisan inertia:middleware
ve app/Http/Kernel.php
dosyasına middleware'i dahil et
Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\HandleInertiaRequests::class,
],
];
Vue.js kullanıyorsan, Vue ve Inertia'yı projeye dahil et
bash
npm install vue@3
resources/js/app.js
dosyanı oluştur veya düzenleapp.js
import { createApp, h } from 'vue'
import { createInertiaApp } from '@inertiajs/vue3'
createInertiaApp({
resolve: name => import(`./Pages/${name}.vue`),
setup({ el, App, props, plugin }) {
createApp({ render: () => h(App, props) })
.use(plugin)
.mount(el)
},
})
resources/views/app.blade.php
dosyanı oluşturapp.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inertia.js Laravel</title>
@vite(['resources/js/app.js'])
</head>
<body>
@inertia
</body>
</html>
Bir Inertia sayfası oluşturalım:
resources/js/Pages/Home.vue
bash
php artisan make:controller HomeController
HomeController.php
namespace App\Http\Controllers;
use Inertia\Inertia;
use Inertia\Response;
class HomeController extends Controller
{
public function index(): Response
{
return Inertia::render('Home', [
'message' => 'Laravel ve Inertia ile çalışıyor!'
]);
}
}
wep.php
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index']);
Link
bileşeni ile yönlendirme yapabilirsinHeader.vue
<template>
<div>
<h1>Ana Sayfa</h1>
<inertia-link href="/about">Hakkında</inertia-link>
</div>
</template>
<script setup>
import { Link as InertiaLink } from '@inertiajs/vue3'
</script>
Laravel'e form gönderirken Inertia.post()
kullanılabilir:
Contact.vue
)Header.vue
<script setup>
import { ref } from 'vue'
import { useForm } from '@inertiajs/vue3'
const form = useForm({
name: '',
email: '',
message: ''
})
const submit = () => {
form.post('/contact')
}
</script>
<template>
<form @submit.prevent="submit">
<input v-model="form.name" placeholder="Adınız" />
<input v-model="form.email" type="email" placeholder="E-posta" />
<textarea v-model="form.message" placeholder="Mesajınız"></textarea>
<button type="submit">Gönder</button>
</form>
</template>
ContactController.php
)ContactController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Inertia\Inertia;
class ContactController extends Controller
{
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
]);
// Burada verileri kaydedebilir veya e-posta gönderebilirsin
return redirect()->back()->with('success', 'Mesajınız gönderildi!');
}
}
web.php
)web.php
use App\Http\Controllers\ContactController;
Route::post('/contact', [ContactController::class, 'store']);