简体   繁体   English

通过href发送数据获取Route Methode

[英]Sending data via href get Route Methode

I build get Methode route to store data via controller in a mysql database. 我建立了get Methode路由,以通过控制器将数据存储在mysql数据库中。 Here you can see a part of my view 在这里您可以看到我的观点

@foreach($groupsrequest as $grouprequest)
        <a class="btn btn-primary btn-block" href="{{ route('mgmtgroups_approvel', ['idgroup' => $grouprequest->idgroup, 'iduser' => $grouprequest->iduser]) }}">               
            Gruppe <strong>{{$grouprequest->group_name}}</strong> Anfrage von User <strong>{{$grouprequest->username}}</strong>
        </a>
@endforeach

And this is my web route file which pass the data to controller where the data is going to store into the mysql db. 这是我的网络路由文件,该文件将数据传递到控制器,数据将存储到mysql db中。

Route::get('/home/groupmgmt/approvel', 'GroupController@setGroupApprovel')->name('mgmtgroups_approvel');

Now my problem, after i clicked on the a href button I get the following url 现在我的问题是,在单击href按钮后,我得到了以下网址

http://localhost:8000/home/groupmgmt/approvel?idgroup=18445&iduser=123

and i can change the url parameter and press enter and then data will be stored. 我可以更改url参数并按Enter,然后将存储数据。 But this cannot be correct, because I am able to store fake data in the db. 但这是不正确的,因为我能够在数据库中存储伪数据。 I can for example create userid which not exist. 例如,我可以创建不存在的用户ID。 How to do this in the right way that this is not possible and only data which are shown in the view before can be stored in the db? 如何以不可能的正确方式来做到这一点,并且只能将以前在视图中显示的数据存储在数据库中?

You can use exists validation type of laravel to validate existence of user ID. 您可以使用laravel的现有验证类型来验证用户ID的存在。

BTW, if the ID is ID of logged in user then you don't need to pass it via query parameters, you can just get it using Auth::id() function of laravel. 顺便说一句,如果ID是已登录用户的ID,那么您不需要通过查询参数传递它,您只需使用laravel的Auth::id()函数Auth::id()获取它。

Also I suggest you to use POST method instead of GET and add csrf token to the request to improve the security. 另外,我建议您使用POST方法而不是GET并将csrf令牌添加到请求中以提高安全性。 Also you should validate the request to make sure nobody can insert invalid data to the database. 另外,您应该验证请求,以确保没有人可以将无效数据插入数据库。 For example if this is a registration approval script, you should make sure that the given ID is requested for registration on group otherwise you should show an error to the user. 例如,如果这是注册批准脚本,则应确保请求给定的ID进行组注册,否则应向用户显示错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM