skip to Main Content

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.

https://phpout.com/wp-content/uploads/2024/01/EGTwr.png

2

Answers


  1. i just tested this with a test route

    Route::get('test/{id}', function (Request $request, $id) {
    
     try {
           $decrypted = decrypt($request);
           dd($decrypted);
       } catch (IlluminateContractsEncryptionDecryptException $e) {
           dd('i was able to see error', $e);
           return redirect()->route('AdminUsers.users_overview')->with('error', 'Something went wrong!');
       }
    
    });
    

    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

    Login or Signup to reply.
  2. Your route parameter is named id:

    Route::get('/domain/users/test/{id}'
    

    which means your controller function expects a variable named $id, not $request.

    public function users_test($id) {
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search