<hr/>

<h2>
    <a name="step-7-render-a-vue-page-with-inertia" href="#step-7-render-a-vue-page-with-inertia">
    </a>
    Adım 7 — Inertia ile Bir Vue Sayfası Render Et
</h2>

<p>Normal Inertia “sayfa rotanızı” <code>routes/web.php</code> içinde tutuyoruz:<br/></p>

<div class="highlight js-code-highlight">
<pre class="highlight php"><code><span class="kn">use</span> <span class="nc">Illuminate\Support\Facades\Route</span><span class="p">;</span>

use Inertia\Inertia;

Route::get(‘/users/{id}’, function (string $id) {
return Inertia::render(‘UserShow’, [
‘id’ => $id,
]);
});

<p>Artık Vue sayfanız (<code>resources/js/Pages/UserShow.vue</code>) <code>/api/users/{id}</code> üzerinden verileri çekebilir.</p>

<p>Temiz bir Inertia dostu versiyon, prop'ları (Vue Router'a gerek olmadan) kullanır:<br/></p>

<div class="highlight js-code-highlight">
<pre class="highlight vue"><code><span class="nt"><span class="k">script</span> <span class="na">setup</span> <span class="na">lang=</span><span class="s">"ts"</span><span class="nt">&gt;</span>

import { computed, onMounted, ref } from vue;

type User = { id: string; name: string};

const props = defineProps{ id: string}>();
const id = computed(() => props.id);

const loading = ref(true);
const error = refstring | null>(null);
const user = refUser>({ id: “”, name: “” });

onMounted(async () => {
try {
const res = await fetch(/api/users/</span><span class="p">${</span><span class="nf">encodeURIComponent</span><span class="p">(</span><span class="nx">id</span><span class="p">.</span><span class="nx">value</span><span class="p">)}</span><span class="s2">);
if (!res.ok) throw new Error(HTTP </span><span class="p">${</span><span class="nx">res</span><span class="p">.</span><span class="nx">status</span><span class="p">}</span><span class="s2">);
user.value = await res.json();
} catch (e: any) {
error.value = e?.message ?? Bilinmeyen hata;
} finally {
loading.value = false;
}
});
script>

template>

class=“p-4”>

<span class="nt"><p> <span class="na">v-if=</span><span class="s">"loading"</span><span class="nt">&gt;</span>Yükleniyor...<span class="nt"/></p></span>
<span class="nt"><p> <span class="na">v-else-if=</span><span class="s">"error"</span> <span class="na">class=</span><span class="s">"text-red-600"</span><span class="nt">&gt;</span><span class="si">{{</span> <span class="nx">error</span> <span class="si">}}</span><span class="nt"/></p></span>

<span class="nt"><div> <span class="na">v-else</span> <span class="na">class=</span><span class="s">"mt-3"</span><span class="nt">&gt;</span>
  <span class="nt"><p><b>ID:<span class="nt"/></b> <span class="si">{{</span> <span class="nx">user</span><span class="p">.</span><span class="nx">id</span> <span class="si">}}</span><span class="nt"/></p></span>
  <span class="nt"><p><b>İsim:<span class="nt"/></b> <span class="si">{{</span> <span class="nx">user</span><span class="p">.</span><span class="nx">name</span> <span class="si">}}</span><span class="nt"/></p></span>
<span class="nt"/></div></span>


template>

<p>Artık açabilirsiniz:</p>

<ul>
<li><code>http://127.0.0.1:8000/users/1</code></li>
</ul>

<p>…ve bu, kullanıcı verilerini tam zincir üzerinden yükleyerek gösterir:<br/>Vue → Laravel API → gRPC istemcisi → gRPC sunucusu.</p>

<hr/>
<h2>
    <a name="step-8-seed-fake-users" href="#step-8-seed-fake-users">
    </a>
    Adım 8 — Sahte kullanıcıları ekle
</h2>

<p>Zaten Laravel veritabanınızı kullanıyorsanız, en kolay yaklaşım:</p>

<ol>
<li>Bir seeder (veya factory) oluşturun</li>
<li>Migrate + seed çalıştırın</li>
</ol>

<p>Örnek:<br/></p>

<div class="highlight js-code-highlight">
<pre class="highlight shell"><code>php artisan migrate <span class="nt">--seed</span>

<p>gRPC sunucunuz aynı SQLite DB dosyasını okuyorsa, gerçek ekli satırları dönebilir.</p>

<hr/>

<h2>
    <a name="run-everything-local" href="#run-everything-local">
    </a>
    Her Şeyi (yerel) Çalıştır
</h2>

<p>Terminal 1 — gRPC sunucusu:<br/></p>

<div class="highlight js-code-highlight">
<pre class="highlight shell"><code><span class="nb">cd </span>grpc-server

node server.js

<p>Terminal 2 — Laravel + Vite:<br/></p>

<div class="highlight js-code-highlight">
<pre class="highlight shell"><code>composer <span class="nb">install

cp .env.example .env
php artisan key:generate

npm install
npm run dev

php artisan serve

<hr/>

<h2>
    <a name="when-is-this-pattern-worth-it" href="#when-is-this-pattern-worth-it">
    </a>
    Bu desen ne zaman değerlidir?
</h2>

<p>Bu yapılandırmayı kullanın:</p>

<ul>
<li>iç hizmetler arasında <strong>tipli sözleşmeler</strong> istiyorsanız</li>
<li>farklı dillerde birden fazla backendiniz varsa</li>
<li>takımlar arasında paylaşılan <strong>kararlı bir arabirim</strong> (proto) istiyorsanız</li>
<li>performans ve akış özellikleri istiyorsanız (gRPC akış desteği sunar)</li>
</ul>

<p>Uygulamanız küçük bir monolit ise ve yalnızca herkese açık API'nizle ilgileniyorsanız, REST hala daha basit olabilir.</p>

<p>Kaynak: <a href="https://github.com/VincentCapek/laravel-vue-grpc-bff" target="_blank" rel="noopener noreferrer">https://github.com/VincentCapek/laravel-vue-grpc-bff</a></p>

Kaynak: Orijinal Makale

Bu Makaleyi Paylaş