dpn517111 2013-12-10 17:30
浏览 42
已采纳

cakephp中的路由不按预期工作

I have a MessageController and I want users to be able to visit the messages/add/ action, passing an id as a variable ie messages/add/5 but for this id not to be visible in the url.

From what I read I believe the following should work:

Router::connect('/messages/add/*', array('controller' => 'messages', 'action' => 'add'));

My understanding is that anywhere that there's a link to messages/add/5 it would appear as a link to messages/add/ and when the user visits messages/add/5 it should show in the address bar as messages/add/. This doesn't work for me.

Do I not understand properly or am I doing something wrong?

Thanks!

  • 写回答

1条回答 默认 最新

  • donglv6747 2013-12-10 17:37
    关注

    That's not how routes work. If you do

    Router::connect('/messages/add/*', array('controller' => 'messages', 'action' => 'add'));
    

    that means that any url with this pattern /messages/add/* will be redirected to your add action in MessagesController (without the variable).

    If you want to pass the id variable without showing it in the address bar, your option is to pass it through POST. Or, you could use ajax to call that url, but anyone with a browser console could look that url and access it too (if you don't have the proper security).

    The point of routes.php is to match any url in the address bar to the set of patterns in the first parameter of the connect array and direct it to the designated action. In no case does the Router changes the address bar setting routes.

    [EDIT]: other option is to pass a slug or hash instead of the plain id. It all depends on what do you want to do and how you rather do it. If you only want the id not to be so blatantly obvious when reading a message, then hash it, or encrypt it. If the add action is to add a message (that's what I think it is), then you shouldn't be passing form parameters as GET variables in the url in the first place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大