How to encrypt laravel 5.2 URL or Routes?
You can encrypt your url parameter and decrypt it in your controller. You can try this:
In your view: Suppose your parameter is id or more parameter you can encrypt.
<?php $parameter =[ 'id' =>1, ]; $parameter= Crypt::encrypt($parameter);?><a href="{{url('/url/',$parameter)}}" target="_blank">a link</a>
Your route will be:
Route::get('/url/{parameter}', 'YourController@methodName');
In your controller, You can decrypt your parameter:
public function methodName($id){ $data = Crypt::decrypt($id); }
You must be yous Crypt namespace in your top of controller
use Illuminate\Support\Facades\Crypt;
Note: You can encrypt url parameter with Crypt::encrypt($parameter)
and decrypt with Crypt::decrypt($parameter)
One way you could mitigate this issue would be to use Universally Unique ID's (UUID).
You will no longer have the issue of auto-increment database crawling and a user cannot alter URL's to get different data.
You can quite easily change your database to support this in your migrations by changing your id column from
this:
$table->increments('id');
to this:
$table->uuid('id')->primary();
Your model can then be edited to support the non incrementing primary key by adding the following to your class:
protected $incrementing = false;
You can encrypt the route in your controller while redirecting, using
\Crypt::encrypt(product_id)
and on the product page you can decrypt the product ID from the URL using
$product_id = \Crypt::decrypt($url_parameter)
that's the best possible way.
But there will be some chances of exception if the user Edit's the Product ID parameter from the URL which you will need to handle.