如何设置路由Zend框架1.12
问题描述:
我的目标是能有像产品链接:如何设置路由Zend框架1.12
domain.com/test-product
domain.com/second-test-product
代替:
domain.com/products/product/id/5
domain.com/products/product/id/123
信息大约每一件产品得到ProductsController
在productAction()
。
它工作正常:
的ProductsController:
public function productAction() {
$products = new Application_Model_DbTable_Products();
$nicelink = $this->_getParam('nicelink', 0);
$this->view->product = $products->fetchRow($products->select()->where('product_nicelink = ?', $nicelink));
// nicelink is always unique
}
这种方法的链接看起来像:
for ($i=0; $i < count($this->products); $i++) {
echo '<a href="' .$this->baseUrl. '/' .$this->products[$i]->product_nicelink. '">LINK</a>';
}
信息有关每个产品显示在product.phtml
观点:
<?php echo $this->escape($this->product->product_name); ?>
而且我的bootstrap.php文件:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initRoutes() {
$router = Zend_Controller_Front::getInstance()->getRouter();
include APPLICATION_PATH . "/configs/routes.php";
//$frontController = Zend_Controller_Front::getInstance();
$route = new Zend_Controller_Router_Route_Regex(
'(.+)',
array(
'controller' => 'products',
'action' => 'product'
),
array(
1 => 'nicelink',
),
'%s.html'
);
$router->addRoute('profileArchive', $route);
}
}
然而,这种解决方案有1个主要缺点:所有其他环节如
domain.com/contact
domain.com/about-us
不工作(可能是由于引导文件)。 如何解决问题,以使另一个链接工作,并保持当前的产品链接?
答
问题是,您正在匹配产品控制器中产品操作的所有路线。可能你想继续在zend 1中使用默认路由,它将“contact”和“about-us”匹配到默认控制器中的相应操作。
如果选中“Zend_Controller_Router_Rewrite和”功能“addDefaultRoutes()”和“路线()”,你可以看到,默认路由将任何自定义的后进行检查。
最简单的办法,找对你的要求将匹配以“ - 产品”结尾的路线:
$route = new Zend_Controller_Router_Route_Regex(
'(.+)\-product',
array(
'controller' => 'products',
'action' => 'product'
),
array(
1 => 'nicelink',
),
'%s.html'
);
在这种情况下nicelink应该采取价值观“测试”和“第二次测试“在你的相应例子中。
但是,这是一个好主意,但每个产品网址的末尾总会有'-product'结尾,例如:domain.com/test-seo-url-product。如何摆脱这一点? – Peter