Merge URL parameters and generate a encrypted URL - Laravel 5.3

1.1k views Asked by At

I have a URL like http://localhost:8000/assessment/3/199

Where 3 represents assignment id and 199 represents assessor id, in short they both represents two models.

I'm sending such URL into email. I want to first encrypt the URL and then send it to the email.

I want URL like http://localhost:8000/assessment/{some-long-random-string}

So, I want to merge both the parameters, make an encrypted string, send into email, upon accessing the URL decrypt it and get both actual parameters.

I would like a solution which uses Laravel to implement that. May be using these:

https://laravel.com/docs/5.3/encryption

https://laravel.com/docs/5.3/hashing

1

There are 1 answers

1
Daan Meijer On

The way I would tackle this, is by manually implementing it in a controller. Generate the URL like this:

$url = URL::action('AssessmentController@decrypt', json_encode([
    'assessment' => $assessment_id, 
    'assessor' => $assessor_id
]);
//now, use $url in your email

Create a route like:

Route::get('/assessment/{ciphertext}', 'AssessmentController@decrypt');

In the controller:

public function decrypt($ciphertext){

    $params = json_decode(decrypt($ciphertext));
    return $this->getAssessment($params['assessment'], $params['assessor']);

}

Of course, you will need more integrity-checks and some error handling, but you probably get the idea.