代码之家  ›  专栏  ›  技术社区  ›  Adam Lesniak

CodeIgniter隐藏帖子id,只显示slug URL

  •  0
  • Adam Lesniak  · 技术社区  · 11 年前

    我正在开发基于CodeIgniter的自定义博客。在我获得url的那一刻遇到了一些问题:

    /博客/视图/1/我的第一篇文章

    我对此不满意,我想去掉id和“/view”

    这就是我的控制器的样子:

      function index($postId=null)
            {
                $this->view($postId=null);
            }
    
    
            function view($postId, $str_slug = '')
            {
                    $data['title'] = ucfirst("Blog");
                    $data['post'] = $this->posts->get_posts($postId);
                    if($postId !== null)
                    {
                            $this->load->view('templates/head', $data);
                            $this->load->view('templates/header', $data);
                            $this->load->view('posts/single_view', $data);
                            $this->load->view('templates/footer',$data);         
                    } else {
                            $this->load->view('templates/head', $data);
                            $this->load->view('templates/header', $data);
                            $this->load->view('posts/index', $data);
                            $this->load->view('templates/footer',$data);
                    }
    
                    $row = $this->db->get_where('posts', array('id' => $postId))->row();
    
                    if ($row and ! $str_slug) {
    
                        $str_slug = url_title($row->title, 'dash', TRUE);
                        redirect("blog/view/{$postId}/{$str_slug}");
    
                    }               
            }
    

    实现这一目标的最佳方法是什么?

    谢谢 亚当

    2 回复  |  直到 11 年前
        1
  •  2
  •   Petre Pătraşc    11 年前

    我想你在找 _重映射() 方法您可以在此处阅读更多信息: http://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping

    您的代码如下所示。如果您仍然需要get_posts_by_slug()方法,那么您仍然需要实现它。

    public function _remap($slug) {
        $data['title'] = ucfirst("Blog");
        $data['post'] = $this->posts->get_posts_by_slug($slug);
    
        if($slug !== null)
        {
                $this->load->view('templates/head', $data);
                $this->load->view('templates/header', $data);
                $this->load->view('posts/single_view', $data);
                $this->load->view('templates/footer',$data);         
        } else {
                $this->load->view('templates/head', $data);
                $this->load->view('templates/header', $data);
                $this->load->view('posts/index', $data);
                $this->load->view('templates/footer',$data);
        }
    
        $row = $this->db->get_where('posts', array('slug' => $slug))->row();
    }
    

    使现代化 : 嘿@AdamLesniak-通常情况下,将永久链接存储到博客文章或新闻文章是很好的设计,这样它就不依赖于标题或任何其他不稳定的数据结构,以便仍然可以访问。

    但对于另一种方法,就我个人而言,我真的认为这是一个很好的系统:

    http://localhost/blog/my-news-article-title-permalink/3
    

    主要的问题是,如果你不把slug/permalink存储在某个地方,那么当有人更改文章的标题时,你会感到难过。例如,我的文章名为“Hello World First Blog Post”,可访问:

    http://localhost/blog/hello-world-first-blog-post
    

    更改为“我做过的第一个帖子”:

    http://localhost/blog/first-post-i-ever-made
    

    那么,初始URL会发生什么呢?它不再可访问-任何通过搜索引擎或某人的链接进入网站的用户现在都不会再看到你的评论,而是会得到一个404页面,你想避免这个页面。

    单独使用永久链接的一个问题是,你需要确保它们是唯一的,并且需要为此设置额外的约束。

    你可以做一些技巧,但它们都有陷阱。例如,您可以使用我上面提到的系统,将唯一标识符粘贴在URL的末尾:

    http://localhost/blog/hello-world/3
    

    如果标题发生了变化,你其实并不在乎,因为你不是在使用slug进行搜索,而是依赖于唯一的标识符。

    http://localhost/blog/first-post-i-ever-made/3
    

    然而,我看到有人认为这种系统反对URL的想法( 制服 资源定位器)。我刚开始的时候就用过它,事实证明它是一个灵活的系统;至少试验一下肯定很好。

    BBC通过保留文章所属的类别和条目的唯一标识符来实现上述变体:

    http://www.bbc.co.uk/news/business-24511283
    

    基本上,他们知道一篇文章永远不会改变它的类别,尽管它可能会改变标题,所以他们只保留一般的主题,即 商业 和唯一标识符 24511283 .

    最后,我建议您只生成以下格式,因为这是迄今为止最具可扩展性的解决方案:

    http://localhost/blog/permalink/unique-id
    

    上面的格式可以让你拥有唯一的标识符,这对于保证奇点很重要,而永久链接则对所有搜索引擎友好!现在,如果文章的标题发生了变化,请在页面上向用户显示更新后的标题,但不要对永久链接做任何操作,这样你的URL就永远不会改变。

    通过在URL中使用ID,您可以确信您可以多次使用永久链接,并且您的系统仍将正确扩展。

        2
  •  1
  •   Anthony    11 年前

    我写了一篇关于这方面的教程,可能有帮助,也可能没有帮助: http://www.rappasoft.com/tutorials/14-seo-friendly-links-with-codeigniter.html