Routing Temelleri
Route'lar, gelen HTTP isteklerini ilgili controller veya closure'a yönlendirir. Laravel'de route'lar routes/ klasöründe tanımlanır.
Temel Route Tanımlama
// routes/web.php
use Illuminate\Support\Facades\Route;
// GET request
Route::get('/', function () {
return view('welcome');
});
// POST request
Route::post('/contact', function () {
return 'Form gönderildi';
});
// Diğer HTTP metodları
Route::put('/user/{id}', function ($id) { /* ... */ });
Route::patch('/user/{id}', function ($id) { /* ... */ });
Route::delete('/user/{id}', function ($id) { /* ... */ });
// Birden fazla metod
Route::match(['get', 'post'], '/form', function () { /* ... */ });
// Tüm metodlar
Route::any('/webhook', function () { /* ... */ });
Route Parametreleri
// Zorunlu parametre
Route::get('/user/{id}', function ($id) {
return "User ID: $id";
});
// Birden fazla parametre
Route::get('/post/{post}/comment/{comment}', function ($postId, $commentId) {
return "Post: $postId, Comment: $commentId";
});
// Opsiyonel parametre
Route::get('/user/{name?}', function ($name = 'Misafir') {
return "Merhaba, $name!";
});
// Regex constraint
Route::get('/user/{id}', function ($id) {
return "User ID: $id";
})->where('id', '[0-9]+');
// Birden fazla constraint
Route::get('/user/{id}/{name}', function ($id, $name) {
// ...
})->where([
'id' => '[0-9]+',
'name' => '[a-z]+',
]);
// Global constraint (RouteServiceProvider'da)
Route::pattern('id', '[0-9]+');
Named Routes
Route'lara isim vererek URL üretimi kolaylaşır:
// İsimli route tanımlama
Route::get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('/user/{id}/profile', function ($id) {
// ...
})->name('user.profile');
// URL üretme
$url = route('dashboard');
// /dashboard
$url = route('user.profile', ['id' => 1]);
// /user/1/profile
// Blade'de kullanım
Dashboard
Profil
// Yönlendirme
return redirect()->route('dashboard');
return to_route('user.profile', ['id' => 1]);
Route Grupları
// Prefix ile gruplama
Route::prefix('admin')->group(function () {
Route::get('/dashboard', function () {
// /admin/dashboard
});
Route::get('/users', function () {
// /admin/users
});
});
// Middleware ile gruplama
Route::middleware(['auth', 'verified'])->group(function () {
Route::get('/profile', function () {
// Auth ve verified middleware uygulanır
});
Route::get('/settings', function () {
// Auth ve verified middleware uygulanır
});
});
// Name prefix ile gruplama
Route::name('admin.')->group(function () {
Route::get('/dashboard', function () {
// Route ismi: admin.dashboard
})->name('dashboard');
});
// Birden fazla özellik
Route::prefix('admin')
->name('admin.')
->middleware(['auth', 'admin'])
->group(function () {
Route::get('/dashboard', [AdminController::class, 'dashboard'])
->name('dashboard');
});
Controller'a Yönlendirme
use App\Http\Controllers\UserController;
// Tek metod
Route::get('/users', [UserController::class, 'index']);
Route::get('/users/{id}', [UserController::class, 'show']);
Route::post('/users', [UserController::class, 'store']);
// Resource route (CRUD için)
Route::resource('posts', PostController::class);
// Oluşturulan route'lar:
// GET /posts -> index
// GET /posts/create -> create
// POST /posts -> store
// GET /posts/{post} -> show
// GET /posts/{post}/edit -> edit
// PUT /posts/{post} -> update
// DELETE /posts/{post} -> destroy
// Sadece belirli metodlar
Route::resource('posts', PostController::class)
->only(['index', 'show']);
Route::resource('posts', PostController::class)
->except(['destroy']);
// API Resource (create ve edit hariç)
Route::apiResource('posts', PostController::class);
Route Model Binding
use App\Models\User;
// Implicit binding
Route::get('/user/{user}', function (User $user) {
return $user->email;
});
// Laravel otomatik olarak User::findOrFail($id) yapar
// Custom key
Route::get('/user/{user:email}', function (User $user) {
return $user->name;
});
// User::where('email', $email)->firstOrFail()
// Explicit binding (RouteServiceProvider)
Route::model('user', User::class);
Route Caching
# Production için route cache
php artisan route:cache
# Cache temizleme
php artisan route:clear
# Route listesi görüntüleme
php artisan route:list
php artisan route:list --path=api
php artisan route:list --name=admin
Önemli Noktalar
- routes/web.php web route'ları, routes/api.php API route'ları içerir
- GET, POST, PUT, PATCH, DELETE metodları desteklenir
- Route parametreleri {} içinde tanımlanır
- Named routes ile URL'ler dinamik oluşturulur
- Route grupları ile DRY prensibi uygulanır