【问题标题】:Zend Framework problem with URL mappingURL 映射的 Zend Framework 问题
【发布时间】:2009-01-05 14:11:48
【问题描述】:

我正在尝试将操作映射到基本 URL 和默认控制器。

发誓这应该是直截了当的,甚至是“开箱即用”的功能,但是如何使用 Zend 框架将默认控制器中的操作映射到基本 url?我对框架还很陌生,所以我希望我只是遗漏了一些明显的东西。

尝试映射:

domain.com/index/my-page 到 domain.com/my-page

默认情况下不会破坏任何其他路由设置。

我可以使用 Zend_Router 破解它,但它违反了其他规则,例如/:controller/:action /:module/:controller/:action

注意:/index/my-page 是一个示例 url - 我需要它来动态处理所有 indexController 操作。

'tharkun' 要求的 URL 映射示例 indexController 有方法 indexAction 和 contactAction 需要网址

/index
/index/contact
/
/contact

第二个控制器 testController 有方法 indexAction 和 monkeyAction 需要网址

/test
/test/index
/test/monkey

基本上 - 如果系统找不到 VAR 的控制器,那么它会在默认控制器中查找 VAR 的操作

【问题讨论】:

  • 道歉应该更清楚,这只是我的示例 url - 我需要它动态地为所有 indexController 操作工作,当我添加正则表达式和捕获时,它开始破坏其他默认路由器。

标签: php zend-framework


【解决方案1】:

默认控制器是 IndexController。

(默认)映射的工作方式如下:

/ => IndexController::indexAction
/index => IndexController::indexAction
/index/foo => indexController::fooAction
/foo => FooController::indexAction

所以,像这样添加一个用户定义的路由(优先级低于默认)

$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
    '/:action',
    array(
        'controller' => 'index'
    )
);
$router->addRoute('user', $route);

这并没有破坏我对默认路由的使用。

编辑:正如coffeerings 在评论中所说,这将破坏非默认控制器的indexAction。

【讨论】:

  • 几乎 :) 它适用于除其他控制器中的默认操作之外的所有操作。因此 indexController 方法 /、/index 和 /testme 有效,但在 testController 中只有 /test/index 和 /test/testme 有效,默认 /test/ 操作失败。
  • 啊——在我的第一个小 Zend 应用程序中,我实际上并没有使用另一个控制器的 index 方法。
猜你喜欢
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-22
  • 2016-03-01
  • 2017-10-02
相关资源
最近更新 更多