Zend_Router 参数异常

发布于 2024-09-27 19:35:32 字数 1733 浏览 1 评论 0原文

我的问题是我想要一些参数值,通过 URL 传递,不会触发 Zend 路由,但会导致默认的控制器/操作对。

现在我的index.php中有以下内容:

    // *** routing info ***
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('showpage', new Zend_Controller_Router_Route('/show/:title',
                                                               array('controller' => 'Show',
                                                                       'action' => 'page')));
// annoying exceptions :(
$router->addRoute('addshow', new Zend_Controller_Router_Route('/show/add',
                                                               array('controller' => 'Show',
                                                                       'action' => 'add')));
$router->addRoute('saveshow', new Zend_Controller_Router_Route('/show/save',
                                                               array('controller' => 'Show',
                                                                       'action' => 'save')));
$router->addRoute('addepisode', new Zend_Controller_Router_Route('/show/addEpisode',
                                                               array('controller' => 'Show',
                                                                       'action' => 'addEpisode')));
$router->addRoute('saveepisode', new Zend_Controller_Router_Route('/show/saveEpisode',
                                                               array('controller' => 'Show',
                                                                       'action' => 'saveEpisode')));

没有最后4个路由器,URL /show/add 导致show/page,带有title == 'add'。 请,我们将非常感谢您的每一次帮助。

My problem is I want some parameter values, passed through URL, don't trigger the Zend routing but lead to defaul controller/action pair.

Right now I have following in my index.php:

    // *** routing info ***
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('showpage', new Zend_Controller_Router_Route('/show/:title',
                                                               array('controller' => 'Show',
                                                                       'action' => 'page')));
// annoying exceptions :(
$router->addRoute('addshow', new Zend_Controller_Router_Route('/show/add',
                                                               array('controller' => 'Show',
                                                                       'action' => 'add')));
$router->addRoute('saveshow', new Zend_Controller_Router_Route('/show/save',
                                                               array('controller' => 'Show',
                                                                       'action' => 'save')));
$router->addRoute('addepisode', new Zend_Controller_Router_Route('/show/addEpisode',
                                                               array('controller' => 'Show',
                                                                       'action' => 'addEpisode')));
$router->addRoute('saveepisode', new Zend_Controller_Router_Route('/show/saveEpisode',
                                                               array('controller' => 'Show',
                                                                       'action' => 'saveEpisode')));

without last 4 routers, URL /show/add leads to show/page, carrying title == 'add'.
Please, every help will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我早已燃尽 2024-10-04 19:35:32

您可以使用正则表达式来拒绝添加、保存、addEpisode 和 saveEpisode

$router->addRoute(
  'showpage', 
  new Zend_Controller_Router_Route(
    '/show/:title',
    array(
      'controller' => 'show',
      'action' => 'page'
    ),
    array(
      'title' => '(?:(?!add)(?!save)(?!addEpisode)(?!saveEpisode).)+'
    )
  )
)

You can use a regular expression to reject add, save, addEpisode and saveEpisode

$router->addRoute(
  'showpage', 
  new Zend_Controller_Router_Route(
    '/show/:title',
    array(
      'controller' => 'show',
      'action' => 'page'
    ),
    array(
      'title' => '(?:(?!add)(?!save)(?!addEpisode)(?!saveEpisode).)+'
    )
  )
)
神经暖 2024-10-04 19:35:32

首先,使用 Zend_Controller_Router_Route_Static 用于静态路由。

其次,我很确定您不需要包含前导斜杠,尽管我不确定这是否是一个问题。

由于路由以相反的顺序匹配,你的应该可以工作(我认为)。对于任何不匹配“saveEpisode”、“addEpisode”、“save”或“add”的内容,它应该落入“showpage”路线。

我唯一能想到的另一件事就是使“showpage”路线更加具体,例如

'show/page/:title'

First, use Zend_Controller_Router_Route_Static for the static routes.

Secondly, I'm pretty sure you don't need to include the leading forward slash, though I'm not sure if this is an issue.

As routes are matched in reverse order, yours should work (I think). For anything not matching "saveEpisode", "addEpisode", "save" or "add", it should fall through to the "showpage" route.

The only other thing I could think of would be to make the "showpage" route more specific, something like

'show/page/:title'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文