Laravel 11 Authentication, Middleware ve Authorization

24–28 Mayıs 2027

Ders Notu

Nerede Çalışıyoruz?

Week 29'da oluşturduğumuz aynı product-app/ projesini aç. Week 32 Product CRUD, database ve Bootstrap layout korunur.

Gerçek proje: product-app/
Referans:     09-laravel/week-33/examples/

examples/ bağımsız Laravel projesi değildir. Artisan/Composer komutlarını product-app/ kökünde çalıştır.

İki Farklı Soru

Authentication → Sen kimsin?
Authorization  → Bu işlemi yapmaya yetkin var mı?

Login olmuş bir user, admin işlemlerini yapmak zorunda değildir.

Login Akışı

Login Form
→ POST /login
→ validation
→ Auth::attempt
→ session
→ authenticated user
→ intended page
if (Auth::attempt($credentials)) {
    $request->session()->regenerate();

    return redirect()->intended(route('products.index'));
}

Week 23'te elle kurduğumuz login/session akışını Laravel standartlaştırır.

Protected Routes

Route::middleware('auth')->group(function () {
    Route::resource('products', ProductController::class)->except('show');
});
Request → auth middleware
├── guest → login
└── authenticated → Controller

Laravel 11'de built-in auth middleware için eski app/Http/Kernel.php kaydı yapma.

Role ve Authorization

Mevcut users table yeni migration ile büyür:

php artisan make:migration add_role_to_users_table --table=users
php artisan migrate
Gate::define('manage-products', function (User $user) {
    return $user->role === 'admin';
});
Gate::authorize('manage-products');

Yetki yoksa Laravel 403 döndürür:

404 → kaynak bulunamadı
403 → kullanıcı biliniyor, işlem yetkisi yok

Blade'de @can ile buton gizlemek kullanıcı deneyimidir. Güvenlik için Controller/Gate kontrolü de bulunmalıdır.

Test Matrisi

Kullanıcı Liste Create/Edit/Delete
guest login'e yönlenir hayır
user evet 403
admin evet evet

Debug Sırası

route:list
→ auth/guest middleware
→ login route adı
→ session / Network 302
→ users.role değeri
→ Gate ability adı
→ Controller authorize kontrolü
→ 403 veya Laravel error satırı

Docs: Authentication, Starter Kits, Middleware, Authorization.

Kontrol Soruları

  1. Authentication ve authorization arasındaki fark nedir?
  2. Guest /products adresine giderse ne olur?
  3. Sadece edit butonunu gizlemek neden yeterli değildir?
  4. 403 ile 404 arasındaki fark nedir?