Reputation: 147
I need to do all url ids encrypted like :
user/edit/1
items/edit/35
posts/details/52
to
user/edit/sdfjk54dfds
items/edit/sdfjk54dfds
posts/details/sdfjk5s4dfds
there is lots of areas like blade
files and in controllers
that id used url('items/edit/2')
and also in controller some function are passed by objects like public function itemedit(Items $items)
.
I tried $encrypt_val = Crypt::encrypt($value)
and $decrypt_val = Crypt::decrypt($encrypt_val );
but I need to do it everywhere.
There is any short way or Middleware function to do it ?
Upvotes: 1
Views: 8215
Reputation: 8008
There's a package called Laravel HashSlug that acts as desired. Similarly to the one in sumit's answer, it's built on Hashids, but design specially to work with urls.
Using the above package all you need to do is add a trait and typehint in controller:
class Post extends Model {
use HasHashSlug;
}
// routes/web.php
Route::resource('/posts', 'PostController');
// app/Http/Controllers/PostController.php
public function show(Post $post){
return view('post.show', compact('post'));
}
Upvotes: 0
Reputation: 15464
Use Laravel Hashids
You can encode id like below
$encoded_id = Hashids::encode($id);
Your URL will be like below
<url>/users/edit/sdfjk54dfds
Hash ID installation guide https://github.com/vinkla/laravel-hashids
Upvotes: 2
Reputation: 3849
You can use Uuid
instead of using integer id
. For this please follow the instruction:
Just create a trait
:
trait Uuids
{
/**
* Boot function from laravel.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{$model->getKeyName()} = Uuid::generate()->string;
});
}
}
and in your model use above trait:
use SomeNamespcaeOfTrait;
class User extends Eloquent
{
use Uuids;
/**
* @var bool
*/
public $incrementing = false;
}
and in your migration use uuid
instead of integer
.
Upvotes: 1