2012-03-07 23 views
0

標題可能會引起誤解,但我試圖做一些非常簡單的事情,但無法弄清楚。Zend在生成視圖前向URL添加參數

可以說,我有一個問題,控制器和顯示操作和問題的id是與我仰望問題細節的主鍵 - 這樣的URL看起來像這樣

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

這工作得很好 - 因此,當該視圖就會生成 - 該URL顯示如上所示。

在表演動作

現在,我想要做的是,追加問題的標題(這是我從數據庫中獲取)的URL - 所以當生成視圖 - 該網址顯示爲

​​

它就像在堆棧溢出 - 如果你把任何問題頁 - 說

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

並回車 問題標題被附加到網址爲

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

非常感謝

回答

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

在你的控制器,你需要確保標題參數被設置,或者如果不是,則使用標題集重定向到自己:

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的一部分 - 但是用戶可能只需將URL輸入爲www.example.com/question/show/123 - 但沒有標題 - 因此標題不是必需的。但是我想要的是,即使用戶輸入了上面的網址 - 頁面呈現時 - 網址應該有標題。如果你輸入http://stackoverflow.com/questions/5451200/就像在stackoverflow - 標題會自動附加。那麼這就是你在控制器裏做什麼。 – Gublooo 2012-03-07 10:28:12

+0

是的,標題是可選的提供,但如果沒有提供,控制器將301永久重定向到相同的URL,但附加了標題。你必須做一個重定向,你不能改變url的外觀。 – PatrikAkerstrand 2012-03-07 10:30:37

+0

哦謝謝我剛剛看到您的回覆 - 所以當我將該代碼添加到控制器時 - 我將重定向到主頁。 – Gublooo 2012-03-07 10:33:59

0

這是使用301重定向來完成。

獲取問題,過濾掉和/或替換URL非法字符,然後構造新的URL。它傳遞給Redirector助手(在你的控制器:$this->_redirect($newURL);