UUID (Universal Unique Identifier) ile uygulamanızın güvenliğini sağlayabilirsiniz. UUID ile 128-bit hexadecimal unique 5 kolon üretilir. Bu özellik ile eşsiz url adresi üretebilirsiniz. Örnekte de bu kullanım anlatılacaktır.
Öncelikle posts tablosu için migration oluşturalım.
// database/migrations/create_posts_table.php Schema::create('posts', function (Blueprint $table) { $table->id(); $table->uuid('uuid')->unique(); // uuid sutünu eklenir. $table->string('title'); $table->timestamps(); });
Oluşturduğumuz uuid sutünuna, model oluşturulduğunda otomatik içerik eklemek için;
use Illuminate\Support\Str; protected static function boot() { parent::boot(); static::creating(function ($model) { $model->uuid = (string) Str::uuid(); }); }
Son olarak route dosyasında kullanımı da şu şekilde olacaktır;
Route::get('/posts/{post:uuid}', [PostController::class, 'show']) ->name('posts.show');
bunların sonucunda örnek url gösterimleri;
// http://www.example.org/orders/065e9fb3-6bec-494c-9917-4ab8e71750d4 // http://www.example.org/users/d3835dda-e08f-4ed5-baff-756d62a749b2 // http://www.example.org/images/avatar-d54a923b-c5d4-4294-8033-a221a57ef361.jpg
*Blade dosyalarında route() helper’ını kullanıyorsanız bir değişiklik yapmanıza gerek yok.
<ul> @foreach($posts as $post) <li> <a href="{{ route('posts.show', $post) }}"> // Laravel otomatik olarak uuids değerini id ile ilişkilendiriyor. {{ $post->title }} </a> </li> @endforeach </ul>
İlk Yorumu Siz Yapın