【问题标题】:Cakephp 2 plugin routes not working in form actionsCakephp 2 插件路由在表单操作中不起作用
【发布时间】:2020-08-26 22:23:24
【问题描述】:

我正在制作我自己的名为 Rma 的 cakephp 2 插件,我为这个插件制作了一些路线。当我想制作一个链接到编辑页面的表单时,该链接与我的路线文件中的链接不同。在浏览器中是我的路由工作,但是当我更改表单操作或回显 Html 链接时,cakephp 会生成不同的链接。

代码echo $this->Html->url(array('plugin' => 'Rma', 'controller' => 'RmaRequests', 'action' => 'edit', 2)); 给了我以下网址:'/Rma/RmaRequests/edit/2' 而路由文件中的网址是 '/rma/edit/:id'。 Cakephp 给了我错误的 url。

这是我在插件配置文件夹中的 routes.php (app/Plugin/Rma/Config/routes.php):


Router::connect('/rma/new', [
    'plugin' => 'Rma',
    'controller' => 'RmaRequests',
    'action' => 'add'
]);
Router::connect('/rma/edit/:id', [
    'plugin' => 'Rma',
    'controller' => 'RmaRequests',
    'action' => 'edit'
],
    array(
        // order matters since this will simply map ":id" to
        // $articleId in your action
        'pass' => array('id'),
        'id' => '[0-9]+'
    ));

Router::connect('/rma/new/:id/', array(
    'plugin' => 'Rma', 'controller' => 'RmaRequestProducts', 'action' => 'add'),
    array(
        // order matters since this will simply map ":id" to
        'pass' => array('id'),
        'id' => '[0-9]+'
    )
);

我也尝试将路由放在我的应用程序 routes.php 中,但这仍然不能解决问题。

这是我在 app/Config/bootstrap.php 中加载插件的方式: CakePlugin::load('Rma', array('routes' => true, 'bootstrap' => true));

是不是我忘记了什么或者做错了什么?我使用的是 CakePHP 2.5.1 版

【问题讨论】:

  • 您之前可能连接了其他与该 URL 数组匹配的路由,可能是一些包罗万象的路由,例如 /:controller/:action/*
  • 我找到了一条这样的路线。我删除了该路线,但网址仍然相同。我也将路由从 /rma/... 更改为 /rmaplugin/.... 但它仍然不起作用
  • 如果 URL 仍然相同,那么很可能还有一条路由可以捕获它。你必须检查你所有的路线(在整个代码库中搜索Router::connect),如果你无法弄清楚,就把它们都显示出来。
  • 在这里你能找到我完整的 routes.php 文件(app/Config/routes.php):pastebin.com/Pb7VK5kt。顺便说一下主题没有路线
  • 对不起,好久没用CakePHP 2了,完全忘记了它是如何处理命名参数和不匹配路由的,它需要你使用URL数组中的名称,它不会为不匹配的路由抛出错误,但会生成一个备用 URL :( 长话短说,问题可能是您应该在 URL 数组中使用 'id' => 2,而不仅仅是 2

标签: php cakephp


【解决方案1】:

答案,感谢 cmets 中的@ndm:

Url 有一个名为 id 的命名参数。代替: echo $this->Html->url(array('plugin' => 'Rma', 'controller' => 'RmaRequests', 'action' => 'edit', 2));

应该是

echo $this->Html->url(array('plugin' => 'Rma', 'controller' => 'RmaRequests', 'action' => 'edit', 'id'=> 2));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多