Home > front end >  How to use the Hash facade safely in Laravel 8 to use the result as a route parameter
How to use the Hash facade safely in Laravel 8 to use the result as a route parameter

Time:06-28

I'm trying to hash some text and later-on use it as a parameter in a route.

I'm using the Hash facade to hash the text like the following

$hash = Illuminate\Support\Facades\Hash::make($text);

Then I'm passing it as param like this

//web.php
Route::get('profile/{$hashedText}/info', [ProfileController::class, 'info'])->name('profile.info');
//index.blade.php
route('profile.info', $hashedText);

The problem I'm facing is that Hash::make function doesn't always generate a URL friendly result(ie: existence of '/', '?=', '&'...)

I've noticed that the Hash::make function is not constant (if I run it twice with the same text I get different results) so I think I can loop through results till I get a good result.

Is there a good approach to overcome this?

CodePudding user response:

You could just do this:

$thing = strtr(base64_encode($string), ' /=', '._-');

This will base64 encode a string and replace any , / or = with ., _ or -.

Can obviously add other substitutions.

Could combine it with Laravel hashing lib.

  • Related