2012-02-10 160 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