如何加密laravel 5.2 URL或路由? [英] How to encrypt laravel 5.2 URL or Routes?
问题描述
我需要加密此URL中的路由吗?因为我不希望用户通过更改项目ID来访问URL.例如,用户可以将/items/1234更改为/item/5678.尽管项1234和5678属于同一用户,但我仍然想限制行为.我正在尝试对路由进行加密,但是我不确定这是否是正确的方法.有什么建议吗?
I need to encrypt routes in this URL? Because I do not want user to access URL by changing the item id. For example, user can change /items/1234 to /item/5678. Although item 1234 and 5678 belong to the same user, I still want to restrict the behavior. What I am trying to do is encrypting the routes but I am not sure whether this is a proper way or not. Any suggestions?
推荐答案
您可以加密url参数并在控制器中对其进行解密.您可以尝试以下方法:
You can encrypt your url parameter and decrypt it in your controller. You can try this:
在您看来:假设您的参数是id或可以加密的更多参数.
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>
您的路线将是:
Route::get('/url/{parameter}', 'YourController@methodName');
在您的控制器中,您可以解密参数:
In your controller, You can decrypt your parameter:
public function methodName($id){
$data = Crypt::decrypt($id);
}
您必须是控制器顶部的Crypt命名空间
You must be yous Crypt namespace in your top of controller
use Illuminate\Support\Facades\Crypt;
注意:您可以使用Crypt::encrypt($parameter)
加密url参数,并使用Crypt::decrypt($parameter)
Note: You can encrypt url parameter with Crypt::encrypt($parameter)
and decrypt with Crypt::decrypt($parameter)
这篇关于如何加密laravel 5.2 URL或路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!