2012-03-07 8 views
0

タイトルは誤解を招く可能性がありますが、私は非常に単純なことをしようとしていますが、それを理解できません。ビューを生成する前にURLにパラメータを追加するZend

私は質問コントローラおよびshowアクションと質問idが、私は質問の詳細を調べていると、主キーで考えてみましょう - URLがこの

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

のように見えるので、これは正常に動作します - だから、ときビューが生成されます - 上記のようにURLが表示されます。

今showアクションで、私がやりたいことは、URLに(私は、データベースから取得)質問のタイトルを追加している - ので、ビューが発生した場合 - URLが

​​

として表示されますスタックオーバーフローのような

その - 何か質問ページを取る場合 -

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

を言うと、質問のタイトルは

としてURLに付加されます を入力ヒット
 
http://stackoverflow.com/questions/5451200/make-seo-sensitive-url-avoid-id-zend-framework 

どうもありがとう

答えて

2

:($this->_redirect($newURL);あなたのコントローラ内)Redirectorヘルパーに渡しますこのパラメータが常に表示されていることを確認したい場合は、パラメータがコントローラに設定されているかどうかを確認し、欠落している場合はリダイレクトを発行する必要があります。だから、

、ブートストラップファイルに:あなたはこのルートを持っていることを、あなたはそのようなあなたのビューでそれを使用することができます

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が設定されているか、そうでない場合はタイトルセットを使用して自身にリダイレクトする:

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 ...] 
} 
+0

詳細についてはPatrikにお問い合わせください。あなたがコントローラーに加えた変更についてはわかりません。すべての私の見解では、タイトルをURLの一部として設定していますが、タイトルなしでwww.example.com/question/show/123というURLを入力するだけで。しかし、私が望むのは、ユーザーが上記のURLをタイプしても、ページがレンダリングされたときでも、URLにはタイトルが付いているはずです。 stackoverflowのようにhttp://stackoverflow.com/questions/5451200/を入力すると、タイトルが自動的に追加されます。それで、あなたがここでコントローラーでやっていることです。 – Gublooo

+0

ええ、提供するタイトルはオプションですが、提供されていない場合、コントローラは同じURLに301の恒久リダイレクトを行いますが、タイトルは追加されます。あなたはリダイレクトを行う必要があります、あなたはURLの外観を変更することはできません。 – PatrikAkerstrand

+0

ohありがとう私はちょうどあなたの返事を見ました - だから私はコントローラにそのコードを追加する - 私はホームページにリダイレクトされています。 – Gublooo

0

これは、301リダイレクトを使用して行われます。

URLの不正な文字を除外してフィルタリングしたり置換したりして、新しいURLを作成します。もしあれば、あなたはまた、

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

:あなたが好きURLと一緒に暮らすことができない限り、あなたのルータにカスタムルートを追加する必要があります

関連する問題