Zend Framework将URL重写为SEO友好的URL


Zend Framework rewrite urls to SEO friendly URL

使用Zend框架,我正在尝试构建一个对SEO友好的URL

http://ww.website.com/public/index/categories/category/2
应通过http://ww.website.com/public/index/categories/category.html 进行映射

http://ww.website.com/public/index/products/category/2/subcategory/2
应通过http://ww.website.com/public/index/products/product.html 进行映射

在我的引导文件中,我尝试过这个

protected function _initRoutes()
{ 
    $frontController = Zend_Controller_Front::getInstance ();
    $router = $frontController->getRouter ();
    $route = new Zend_Controller_Router_Route_Regex(
        'index/categories/category/('d*)/',
        array('module' => 'default','controller' => 'index','action' => 'categories'), 
        array(1 => 'category'),
        'index/categories/%d-%s.html');
    $router->addRoute('category', $route);
}

现在我的问题是如何重写这个URL?我还使用根据URL中的ID检索产品或类别

$id = $this->_getParam('category', 0 ); 

有人能帮我吗?

我认为您不需要Regex路由来满足您的需求。例如,如果你想要这个网址:http://mysite.com/category/cars.htmlhttp://mysite.com/categories/category/id/2足以使用Zend_Controller_Router_Route

现在只有一个问题,Zend路由器将不知道您要映射的类别的id,为此您需要将您的自定义路由存储到数据库或文件中;每个自定义路由将包含映射的url和所需参数(控制器名称、操作名称、类别id)。

之后,我们可以构建一个函数,将自定义路由注册到引导文件,或者在像这样的插件中:

public function setRoute($params){
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $params = array(
        'controller' => $params['controller'],
        'action'     => $params['action'],
        'id'         => $params['id']
        );
    $route = new Zend_Controller_Router_Route($params['url'], $params);
    $router->addRoute($params['url'], $route);
}

现在假设$params['url']的值为:http://mysite.com/category/cars.html,当从浏览器请求url ZF将匹配我们的自定义路由并将请求转发到$params['controller']中的指定控制器时,并调用$params['action']中的指定操作,并将其设置为来自$params['id']的名为id的参数值,该值可以用$id = $this->_getParam('id',0); 从控制器中检索

我认为你走错了路,因为SEO友好并不意味着.html或.php,如果你的产品名称在基本url后面开始,就像"http://www.promotepassion.com/classical/rock"意思是基本路径,然后是类别名称,然后是产品名称,如果我们使用,那么所有搜索引擎都会快速调用页面。