我应该如何在 mvc 中编辑模型条目?
How should I edit a model entry in mvc?
我正在为 php 框架开发一个使用 phalcon 的小应用程序。我已经实现了多个控制器和模型,但是到目前为止,当我想编辑一个用户时,我使用 link 到 link 到
localhost/myappname/User/edit/11 "user's id"
我被告知这不是最好的方法,我正在尝试不通过 url 传递 id,就像在表单中使用 post 方法一样,但没有到目前为止的成功。
这是编辑或删除条目的唯一正确方法还是有更好的方法?
我试图搜索问题,但不知道如何命名这个问题,所以我还没有找到答案。
您可以使用 sessionStorage。它会将 userId 的值存储在浏览器中,并在他们离开页面后立即删除。
http://www.w3schools.com/html/html5_webstorage.asp
设置在一页上
sessionStorage.userId = 11;
访问另一个
var user = sessionStoarge.userId;
如果您不想让每个人都可以访问编辑页面,您可以通过几种方式做到这一点。
解决方案#1
您可以使用 Phalcon ACL 来阻止没有权限编辑此页面的用户,这样只有被允许的人(如经理)才能编辑用户或其他内容。
解决方案 #2
您可以 crypt/decrypt 用户 ID,因此在 URL 中它不会被人类读取,然后在编辑方法中尝试解密该 ID,如果它不是有效的回显错误。
<?php
use Phalcon\Crypt;
// Create an instance
$crypt = new Crypt();
$key = 'le password';
$user_id = 5;
$encrypt = $crypt->encryptBase64($user_id, $key);
// Use $encrypt for URL like <a href="/User/edit/{{encrypt}}">Edit</a>
// Use decrypt to get the real id of a user
$crypt->decryptBase64($encrypt, $key);
?>
这样用户会看到URL类似
的东西
localhost/myappname/User/edit/nomGPPXd+gAEazAP8ERF2umTrfl9GhDw1lxVvf39sGKF34AFNzok31VdaT/OwADPPJ4XgaUNClQKrlc/2MfaXQ==
有关详细信息,请参阅 Encryption/Decryption
不过我个人的看法是还是用ACL比较好。毕竟ACL就是为了这种东西而生的
Note! If you want to use Encrypt/Decript remember to wrap decryption
in edit method in try/catch
block and catch exception so you don't
get Error if someone tries to guess sone id.
解决方案#3
如果您仍想使用 POST 执行此操作,请不要使用 <a href="...">Edit</a>
,您可以尝试类似的操作:
<form method="POST">
<input type="hidden" name="uid" value="{{ user_id }}"/>
<button type="submit">Edit</button>
</form>
然后在编辑方法中捕获该 ID,例如:
<?php
$user_id = $this->request->getPost("uid");
?>
NOTE! In this way your URL will not contain user id but someone still
can POST another uid so you can try to hide that real user id even
from input type hidden. You can use again crypt/decrypt so input
hidden uid can be crypted and then decrypt post data in method.
我正在为 php 框架开发一个使用 phalcon 的小应用程序。我已经实现了多个控制器和模型,但是到目前为止,当我想编辑一个用户时,我使用 link 到 link 到
localhost/myappname/User/edit/11 "user's id"
我被告知这不是最好的方法,我正在尝试不通过 url 传递 id,就像在表单中使用 post 方法一样,但没有到目前为止的成功。 这是编辑或删除条目的唯一正确方法还是有更好的方法? 我试图搜索问题,但不知道如何命名这个问题,所以我还没有找到答案。
您可以使用 sessionStorage。它会将 userId 的值存储在浏览器中,并在他们离开页面后立即删除。
http://www.w3schools.com/html/html5_webstorage.asp
设置在一页上
sessionStorage.userId = 11;
访问另一个
var user = sessionStoarge.userId;
如果您不想让每个人都可以访问编辑页面,您可以通过几种方式做到这一点。
解决方案#1
您可以使用 Phalcon ACL 来阻止没有权限编辑此页面的用户,这样只有被允许的人(如经理)才能编辑用户或其他内容。
解决方案 #2
您可以 crypt/decrypt 用户 ID,因此在 URL 中它不会被人类读取,然后在编辑方法中尝试解密该 ID,如果它不是有效的回显错误。
<?php
use Phalcon\Crypt;
// Create an instance
$crypt = new Crypt();
$key = 'le password';
$user_id = 5;
$encrypt = $crypt->encryptBase64($user_id, $key);
// Use $encrypt for URL like <a href="/User/edit/{{encrypt}}">Edit</a>
// Use decrypt to get the real id of a user
$crypt->decryptBase64($encrypt, $key);
?>
这样用户会看到URL类似
的东西localhost/myappname/User/edit/nomGPPXd+gAEazAP8ERF2umTrfl9GhDw1lxVvf39sGKF34AFNzok31VdaT/OwADPPJ4XgaUNClQKrlc/2MfaXQ==
有关详细信息,请参阅 Encryption/Decryption
不过我个人的看法是还是用ACL比较好。毕竟ACL就是为了这种东西而生的
Note! If you want to use Encrypt/Decript remember to wrap decryption in edit method in
try/catch
block and catch exception so you don't get Error if someone tries to guess sone id.
解决方案#3
如果您仍想使用 POST 执行此操作,请不要使用 <a href="...">Edit</a>
,您可以尝试类似的操作:
<form method="POST">
<input type="hidden" name="uid" value="{{ user_id }}"/>
<button type="submit">Edit</button>
</form>
然后在编辑方法中捕获该 ID,例如:
<?php
$user_id = $this->request->getPost("uid");
?>
NOTE! In this way your URL will not contain user id but someone still can POST another uid so you can try to hide that real user id even from input type hidden. You can use again crypt/decrypt so input hidden uid can be crypted and then decrypt post data in method.