【问题标题】:CakePHP: Routing ProblemsCakePHP:路由问题
【发布时间】:2012-03-03 06:23:44
【问题描述】:

全部,

我目前正在使用 CakePHP 来彻底改造我当前的 NEWS 网站。我已经或将把所有当前文章转移到我的新站点,它们将采用与当前站点相同的article_id。但是,我意识到了以下问题。

我当前的站点使用以下设置作为 URL:

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

c=references 类别 ID,a=references 文章 ID。我的新设置 (CakePHP) 我利用了 slugs,现在我的文章 URL 显示为:

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

由于我通过 webstats 注意到,我的许多文章都是通过 Facebook 等其他网站上设置的链接访问的,我认为创建一个系统/路由来为我解决这个问题至关重要。

这就是我的想法:

  1. 创建一个检测与上述类似的请求的路由
  2. 使路由将a 值作为参数传递给我的文章控制器中的重定向函数,例如articleRedirect($article_id)
  3. 然后此函数将根据传递的$article_id 在数据库中查找slug 并重定向到新地址(参见下面的函数)

    // 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']);
        }
    }
    

我认为这应该可行。但是,我在路由方面需要一些认真的帮助。谁能推荐一条我可以使用的路线。

非常感谢。

【问题讨论】:

    标签: cakephp routing cakephp-1.3 routes


    【解决方案1】:

    最简单的方法是稍微重新建模您的 URL 以保留文章 ID。例如:

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

    然后您可以创建 1 个 RewriteRule 将所有旧 URL 重写为这个新结构,并使用 1 Route 将它们路由到控制器的操作。这样,只需 2 行配置即可将旧 URL 迁移到新 URL。您的路线将如下所示:

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

    这将调用您的NoticiasController 的查看操作并将文章 ID 从 URL 传递给它。您必须添加到 .htaccess 中的 RewriteRule 应该看起来像这样(未经测试,但只是为了给您一个指针):

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

    【讨论】:

    • 我试过了,但它不起作用。重写规则可能有问题。我会继续尝试不同的东西。如果我输入 www.mydomain.com/?c=135&a=1341,基本上什么都不会发生。它只是访问我的首页。
    • @CaboONE 是的,重写可能应该只是在 / %{QUERY_STRING} 变量上有一个 RewriteCond 。像 RewriteCond %{QUERY_STRING} ^c=[0-9]+&a=([0-9]+)RewriteRule ^/ /noticia/%1/ 这样的东西可能会这样做。
    【解决方案2】:

    感谢 Oldskool 愿意提供帮助。但是,我在实施它时遇到了麻烦。我决定不使用 .htaccess 或 routes 并选择在 app_controller 中实现一个函数,它将为我处理它:

    在我的 app_controller's beforeFilter() 我添加了以下内容:

    //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]);
        }
    }
    

    如果旧网址,上述函数将转移到 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']);
        }
    }
    

    以这种方式处理这类事情可能不是最好的方法,因为每次访问我的网站时它都会调用该函数,但它对我来说没有重大问题。

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 2016-04-07
      • 2016-07-18
      • 2011-05-18
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多