在我的项目中,我有一些页面定义在我的索引控制器,如ie。关于我们页面。为了让我不用输入domain.com/index/about
而是domain.com/about
,我有这个路由:
$route = new Zend_Controller_Router_Route_Static ( 'about', array (
'controller' => 'Index',
'action' => 'about'
) );
$router->addRoute ( 'about', $route );
它起作用了。问题是有时我有6到7页,我必须重复这条路线6到7次。是否有一种方法让我做一个路由,总是从url中删除"index
"?我永远不需要一个包含index
的url。谢谢!
您可以通过避免静态路由类型来编写动态路由:
$route = new Zend_Controller_Router_Route(
'/:action',
array (
'controller' => 'index',
'action' => 'index',
)
);
$router->addRoute('pages', $route);
这将添加一个名为'pages'的路由,它将匹配索引控制器中的任何单个动作。在此路由中定义的动作和控制器只是默认值,因为你没有将控制器作为变量传递,它将始终路由到IndexController。该操作默认为indexAction,但可以被路由覆盖,例如:
/about -> IndexController / aboutAction
/contact -> IndexController / contactAction
etc ...
请记住,这将覆盖任何其他路由,所以你需要正确地构建你的路由继承结构。之后定义的路由将覆盖已经定义的路由。
Zend Framework Standard Router