为什么在Codeigniter中使用Slug?

时间:2017-11-17 07:01:43

标签: codeigniter slug

我有那个代码。我不知道为什么他们使用slug。有人能解释一下吗? 我搜索slug的意思是什么。我认为它就像一个变量,我可以冒险它的名字。但是slug的重要性是什么? yazi_model.php model->

<?php
    class Yazi_model extends CI_Model{
        public function __construct(){
            $this->load->database();
        }

        public function getir_yazilar($slug = FALSE){ //why slug should be false? why we use slug for it?
            if($slug === FALSE){ //is this 'if' is necessary? I can just write last code and it will work.
                $query = $this->db->get('yazilar'); 
                return $query->result_array(); 
            }
            $query = $this->db->get_where('yazilar',array('slug' => $slug));//why slug?
            return $query->row_array();
    }
}

Yazilar.php控制器 - &gt;

<?php
    class Yazilar extends CI_Controller{ 
        public function index(){ //why not use slug
            $veri['baslik'] = 'Son yazılar';
            $veri['yazilar'] = $this->yazi_model->getir_yazilar();
            $this->load->view('tema/header');
            $this->load->view('yazilar/index',$veri);
            $this->load->view('tema/footer');
        }

        public function detay($slug = NULL){ //why slug
            $veri['yazi'] = $this->yazi_model->getir_yazilar($slug); //why slug
            if(empty($veri['yazi'])){
                show_404();
            }

            $veri['baslik'] = $veri['yazi']['baslik'];
            $this->load->view('tema/header');
            $this->load->view('yazilar/detay',$veri);
            $this->load->view('tema/footer');
        }
    }

1 个答案:

答案 0 :(得分:1)

Slugs可用于获取SEO友好网址。

例如: 从:

www.site.com/index.php/blog/view/8

致:

www.site.com/index.php/blog/view/blog-name

将slug存储在我的数据库表中,在一个名为slug的列中,然后找到一个带有slug的帖子,如下所示:

    public function view($slug)
{
    $query = $this->db->get_where('posts', array('slug' => $slug), 1);

    // Fetch the post row, display the post view, etc...
}