简体   繁体   中英

how to use slug in codeigniter

i don't know how to make url like this

tkd/index.php/article/77/Kejurnas-2013

i am getting error with url like this

tkd/index.php/article/77/Kejurnas%202013

this is my controller

public function index($id = null, $slug = FALSE){

    // Fetch the article
    $this->db->where('pubdate <=', date('Y-m-d'));
    $this->data['article'] = $this->mberita->get_by_id($id,$slug);
    //echo '<pre>' . $this->db->last_query() . '</pre>';
   // dump($this->data['article']);


    // Return 404 if not found
    count($this->data['article']) || show_404(uri_string());

    // Redirect if slug was incorrect
    $requested_slug = $this->uri->segment(3);
    $set_slug = $this->data['article']->slug;
    if ($requested_slug != $set_slug) {
        redirect('article/' . $this->data['article']->id . '/' . $this->data['article']->slug, 'location', '301');
    }

    // Load view
    /*add_meta_title($this->data['article']->judul_berita);*/
    $this->data['contents'] = 'article';
    $this->load->view('template/wrapper/mahasiswa/wrapper_article', $this->data);

}

and this is my modal

public function get_by_id($id = 0, $slug = FALSE)
    { 
      if ($id === 0 && $slug === FALSE)
      {
        $query = $this->db->get('$this->tbl_berita');
        return $query->result_array();
      }

      return $this->db->get_where($this->tbl_berita, array('id' => $id, 'slug' => $slug),1)->row();

    } 

please help me how to make the right url. thank you

You can use one of CodeIgniters helper functions url_title()

Takes a string as input and creates a human-friendly URL string. This is useful if, for example, you have a blog in which you'd like to use the title of your entries in the URL.

site_url("articles/{ID}/").url_title({TITLE});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM