I’m passing an encrypted URL to my controller and I want to decrypt the URL. If the decryption fails then I want to redirect to a page and show an error message.
Currently if the decrypt fails if throw the error and dies:
IlluminateContractsEncryptionDecryptException
The payload is invalid.
How do I redirect on fail? below is the function I’m using that isn’t working:
Link
<a href="{{ route('AdminUsers.test', encrypt("test123")) }}">test</a>
Which creates the URL:
/domain/users/test/eyJpdiI6ImFiTmZYRUY5K2ZqR0txeHNjejY2dnc9PSIsInZhbHVlIjoicktVTWVaY0tMSXlWdEhYWjdWbWtsQT09IiwibWFjIjoiMzcxNWY3ODYyMjI5NWQ2Yjg0MzExODBhMGM4NTlmNTUwMGYwZjFiNTFjZjA2Y2VhNWUxMjBlM2I5YmMzMTY2MiIsInRhZyI6IiJ9
Route:
Route::get('/domain/users/test/{id}', 'users_test')->name('AdminUsers.test')->middleware(['auth:admin']);
Controller
public function users_test($request) {
try {
$decrypted = decrypt($request);
} catch (DecryptException $e) {
return redirect()->route('AdminUsers.users_overview')->with('error', 'Something went wrong!');
}
}
If I do:
try {
$decrypted = decrypt($request);
dd($decrypted);
} catch (DecryptException $e) {
return redirect()->route('AdminUsers.users_overview')->with('error', 'Something went wrong!');
}
``
I get "test123"
If I change the URL to:
/domain/users/test/eyJXXXXXXXXXXX
I get:
IlluminateContractsEncryptionDecryptException
The payload is invalid.
If I add a dd($e) in the catch it doesn’t run as it doesn’t get to it as Lavarel throws it’s error first.
2
Answers
i just tested this with a test route
and it worked
my guess is that this is happening in your case because exception is being thrown by a middleware (what do you have in your auth:admin middleware) or the routing layer before the hitting controller.
try with a test route please
Your route parameter is named
id
:which means your controller function expects a variable named
$id
, not$request
.