0

标题可能具有误导性,但我正在尝试做一些非常简单但无法弄清楚的事情。

假设我有一个问题控制器并显示操作,问题 id 是我查找问题详细信息的主键 - 所以 URL 看起来像这样

http://www.example.com/question/show/question_id/101

这工作正常 - 所以当视图生成时 - URL 显示如上所示。

现在在显示操作中,我想做的是,将问题标题(我从数据库中获取)附加到 URL - 所以当生成视图时 - URL 显示为

http://www.example.com/question/show/question_id/101/how-to-make-muffins

就像堆栈溢出一样-如果您有任何问题页面-说

http://stackoverflow.com/questions/5451200/

并按回车 问题标题被附加到 url 为

http://stackoverflow.com/questions/5451200/make-seo-sensitive-url-avoid-id-zend-framework

非常感谢

4

2 回答 2

2

您必须向路由器添加自定义路由,除非您可以使用以下网址:

www.example.com/question/show/question_id/101/{paramName}/how-to-make-muffins

如果要确保始终显示此参数,您还需要检查控制器中是否设置了该参数,如果缺少则发出重定向。

因此,在您的引导文件中:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
  public function _initRoutes ()
  {
    // Ensure that the FrontController has been bootstrapped:
    $this->bootstrap('FrontController');
    $fc = $this->getResource('FrontController');
    /* @var $router Zend_Controller_Router_Rewrite */
    $router = $fc->getRouter();

    $router->addRoutes( array ( 
      'question' => new Zend_Controller_Router_Route (
        /* :controller and :action are special parameters, and corresponds to
         * the controller and action that will be executed.
         * We also say that we should have two additional parameters:
         * :question_id and :title. Finally, we say that anything else in
         * the url should be mapped by the standard {name}/{value}
         */
        ':controller/:action/:question_id/:title/*',
        // This argument provides the default values for the route. We want
        // to allow empty titles, so we set the default value to an empty
        // string
        array (
           'controller' => 'question',
           'action' => 'show',
           'title' => ''
        ),
        // This arguments contains the contraints for the route parameters.
        // In this case, we say that question_id must consist of 1 or more
        // digits and nothing else.
        array (
           'question_id' => '\d+'
        )
      )
    ));
  }
}

现在您有了这条路线,您可以在视图中使用它,如下所示:

<?php echo $this->url(
         array(
            'question_id' => $this->question['id'], 
            'title' => $this->question['title']
         ),
         'question'
      );
      // Will output something like: /question/show/123/my-question-title 
?>

在您的控制器中,您需要确保设置了标题参数,或者如果没有设置标题,则重定向到自身:

public function showAction ()
{
  $question = $this->getQuestion($this->_getParam('question_id'));
  if(!$this->_getParam('title', false)) {
     $this->_helper->Redirector
        ->setCode(301) // Tell the client that this resource is permanently 
                       // residing under the full URL
        ->gotoRouteAndExit(
           array(
             'question_id' => $question['id'],
             'title' => $question['title']
           )
        );
  }
  [... Rest of your code ...]
}
于 2012-03-07T08:58:12.183 回答
0

这是使用 301 重定向完成的。

获取问题,过滤和/或替换 URL 非法字符,然后构建新 URL。将其传递给Redirector助手(在您的控制器中$this->_redirect($newURL);:)

于 2012-03-07T08:43:40.583 回答