fonksiyon tanımlama
1 |
Route::get('foo', function () { return 'Hello World'; }); |
normal tanımlama
1 |
Route::get('/user', 'Kontrollerismi@okontrollerdayeralanfonksiyonismi'); |
direk view’a yönlendirme yapma
1 |
Route::view('/welcome', 'welcome'); |
link id yapısını yakalama
1 |
Route::get('user/{id}', function ($id) { return 'User '.$id; }); |
ikili id link yakalama
1 |
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { }); |
yakalanan değer null olursa ne olacak
1 |
Route::get('user/{name?}', function ($name = null) { return $name; }); |
bu routelarda function olan kısımları controller@fonksiyon yazarak direk oradan da işlem yaptırabilirsiniz.
slug ile yakalanan idlerin sadece harf veya sayılardan oluşmasını da yapabiliriz.
1 2 3 4 5 |
Route::get('user/{name}', function ($name) { })->where('name', '[A-Za-z]+'); Route::get('user/{id}', function ($id) { })->where('id', '[0-9]+'); Route::get('user/{id}/{name}', function ($id, $name) { })->where(['id' => '[0-9]+', 'name' => '[a-z]+']); |
name ekleme
1 |
Route::get('user/profile', 'UserProfileController@show')->name('profile'); |
name yazılmış url’yi yakalama blade için
1 |
<span class="token function">route</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
route ile profil sayfasına değer gönderme
1 |
Route::get('user/{id}/profile', function ($id) { })->name('profile'); $url = route('profile', ['id' => 1]); |
ilgili linkten gelenlere özel işlem yaptırma (bu middleware kafanız karışmasın)
1 2 3 4 |
public function handle($request, Closure $next) { if ($request->route()->named('profile')) { } return $next($request); } |
grup oluşturma ve kullanıcılara özel linkler oluşturma
1 2 3 4 |
Route::middleware(['first', 'second'])->group(function () { Route::get('/', function () { Uses first & second Middleware }); Route::get('user/profile', function () { Uses first & second Middleware }); }); |
prefix oluşturma ( admin/users – admin/profile başında admin bulunan sayfaları listeleyebilirsin.
1 2 3 |
Route::prefix('admin')->group(function () { Route::get('users', function () { Matches The "/admin/users" URL }); }); |