我知道这是一个老问题,但我有一个类似的问题,只是想我应该发布我的解决方案。也许它可以帮助其他人查看这个问题。
我在插件中编写了我的路线,显然你需要将插件添加到引导程序中才能工作;)
class Plugin_RoutesPage extends Zend_Controller_Plugin_Abstract
{
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$front_controller = Zend_Controller_Front::getInstance();
$router = $front_controller->getRouter();
// Page SEO friendly hierarchical urls
$routePageSeoTree = new Zend_Controller_Router_Route_Regex(
'([-a-zA-Z0-9/]+)/([-a-zA-Z0-9]+)',
array(
// default Route Values
'controller' => 'page',
'action' => 'open',
),
array(
// regex matched set names
1 => 'parents',
2 => 'item'
)
);
$router->addRoute('page-seo-tree',$routePageSeoTree);
// only one level
$routeSinglePage = new Zend_Controller_Router_Route_Regex(
'([-a-zA-Z0-9]+)',
array(
// default Route Values
'controller' => 'page',
'action' => 'open',
),
array(
// regex matched set names
1 => 'item'
)
);
$router->addRoute('page-single',$routeSinglePage);
}
}
这就是您可以在控制器的操作中使用它的方式
class PageController extends Zend_Controller_Action
{
public function openAction()
{
// the part of the uri that you are interested in
$item = $this->_request->getParam('item');
}
}
这是一个如何将其包含到引导程序中的快速示例
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initPlugins()
{
$front_controller = Zend_Controller_Front::getInstance();
$front_controller->registerPlugin(new Plugin_RoutesPage(), 1);
}
}
我不得不使用两条路线,因为我们试图查看/打开的当前页面可能没有任何父页面。我确信可能有更好的方法来编写正则表达式,但这对我有用。如果有人知道如何改进正则表达式,请告诉我。