0

i have that code. i don't get why they use slug. can someone explane? i search what is the mean of slug. I think It is like a variable, I can chance it's name. but what is the importance of 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 controller->

<?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');
        }
    }
Aleyna
  • 45
  • 5
  • Possible duplicate of [Using slugs in codeigniter](https://stackoverflow.com/questions/3305786/using-slugs-in-codeigniter) – Xpleria Nov 17 '17 at 07:20
  • useful class: https://github.com/ericbarnes/CodeIgniter-Slug-Library – Alex Nov 21 '17 at 03:28

1 Answers1

1

Slugs can be used for getting SEO Friendly urls.

For example : From:

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

To :

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

store the slugs in my database table, in a column called slug, then find a post with the slug, like this:

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

    // Fetch the post row, display the post view, etc...
}
Blesson Christy
  • 380
  • 3
  • 13