2012-02-10 158 views
1

全部,CakePHP:路由問題

我目前正在使用CakePHP徹底改造我現在的新聞網站。我已經或將要將所有當前的文章轉移到我的新網站,並且它們將與當前網站的article_id一樣。但是,我意識到了以下問題。

我目前的網站使用以下設置爲URL:

http://www.mydomain.com/?c=140&a=4388 

c=引用的類ID和a=引用的文章ID。我的新格局(CakePHP的),我拿蛞蝓的優勢,現在我的文章的網址顯示爲:

http://www.mydomain.com/noticia/this-is-an-article-slug 

因爲,我已經注意到,通webstats,我的許多文章都是通鏈接設置訪問其他網站,如Facebook ,我認爲創建一個能夠幫助我解決這個問題的系統/路由將是至關重要的。

這是我想到的是:

  1. 創建檢測類似於上面
  2. 提到製作的路線傳遞a值作爲參數傳遞給重定向功能在我的文章控制器要求的路線,如articleRedirect($article_id)
  3. 此功能會再看看在數據庫基礎上,通過$article_idslug並重定向到新的地址(見下文功能)

    // Function in articles controller to redirect old site's article url to 
    // new websites url format 
    
    function articleRedirect($article_id = NULL){ 
        $this->Article->recursive = -1; 
        $slug = $this->Article->findById($article_id);   
        if($slug == NULL){ 
         $this->Session->setFlash('Article not found'); 
         $this->redirect('/noticias'); 
        }else{ 
         $this->redirect('/noticia/'.$slug['Article']['slug']); 
        } 
    } 
    

我認爲應該有效。不過,我需要一些認真的路由幫助。任何人都可以建議我可以使用的路線。

非常感謝。

回答

0

感謝OLDSKOOL爲願意幫助。但是,我很難實現它。我決定遠離使用。htaccess的或路線和選擇,以實現app_controller一個函數處理它爲我這樣的:

在我的app_controller'sbeforeFilter()我增加了以下內容:

//Check old url style as in http://www.bravanews.com/?c=123&a=2345 for redirection 
    //to the correct new url style as in http://www.bravanews.com/noticia/news-title-using-slug 
    $this->CheckOldUrl(); 

然後在我的app_controller.php我創建了以下功能:

function CheckOldUrl(){ 
    preg_match("/\?c=(\w+)\&a=(\w+)/i", $_SERVER['REQUEST_URI'], $matches); 
    if(!$matches == NULL){ 
     $this->redirect('/noticias/articleRedirect/'.$matches[2]); 
    } 
} 

如果舊的URL上面的功能將轉移到articleRedirect功能,那麼這將重定向到正確的文件

// Function in articles controller to redirect old site's article url to 
// new websites url format 

function articleRedirect($article_id = NULL){ 
    $this->Article->recursive = -1; 
    $slug = $this->Article->findById($article_id);   
    if($slug == NULL){ 
     $this->Session->setFlash('Article not found'); 
     $this->redirect('/noticias'); 
    }else{ 
     $this->redirect('/noticia/'.$slug['Article']['slug']); 
    } 
} 

這可能不是最好的方式來處理這種事情,因爲它每次訪問我的網站時都會調用該函數,但它對我來說沒有任何問題。

3

最簡單的方法是重新建模您的URL,稍微保留文章ID。例如:

http://www.example.com/noticia/4388/this-is-an-article-slug 

然後您可以創建1個單重寫規則重寫所有舊的URL的這種新的結構,並使用1條路線路由到您的控制器的作用。這樣只需要2行配置就可以將舊網址遷移到新網址。然後,您的路線將是這個樣子:

Router::connect(
    '/noticia/:article-id/:slug', 
    array('controller' => 'noticias', 'action' => 'view'), 
    array('pass' => 'article-id'), 
    'article-id' => '[0-9]+' 
); 

那會打電話給你NoticiasController的視圖操作,並從URL將它傳遞的文章編號。你必須重寫規則將添加到您的.htaccess應該是這個樣子(未經測試,但只給你一個指針):

RewriteRule ^/?c=[0-9]+&a=([0-9]+)$ /noticia/$1/ 
+0

我試過了,但它不起作用。重寫規則可能有問題。我會繼續嘗試不同的事情。如果我輸入www.mydomain.com/?c=135&a=1341,基本上什麼都不會發生。它只是訪問我的首頁。 – 2012-02-11 19:12:18

+0

@CaboONE是的,重寫可能只是在%{QUERY_STRING}變量上使用RewriteCond。像'RewriteCond%{QUERY_STRING}^c = [0-9] +&a =([0-9] +)'和'RewriteRule ^// noticia /%1 /'可能會這樣。 – Oldskool 2012-02-11 20:47:07

+0

我仍然無法做到。我發佈了我決定使用的解決方案... – 2012-02-12 22:49:39