0

タイトルは誤解を招くかもしれませんが、私は非常に単純なことをしようとしていますが、それを理解することはできません。

質問コントローラーがあり、アクションを表示し、質問IDが質問の詳細を検索するための主キーであるとします。したがって、URLは次のようになります。

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

これは正常に機能します-したがって、ビューが生成されると-URLは上記のように表示されます。

ここで、showアクションで、質問のタイトル(データベースから取得)をURLに追加します。つまり、ビューが生成されると、URLは次のように表示されます。

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

それはスタックオーバーフローのようです-あなたが質問ページを取るなら-言う

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

Enterキーを押すと、質問のタイトルがURLに追加されます。

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

どうもありがとう

4

2 に答える 2

2

次のようなURLを使用できる場合を除き、ルーターにカスタムルートを追加する必要があります。

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 
?>

コントローラで、title-parameterが設定されていることを確認する必要があります。設定されていない場合は、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 に答える