The term Slug it meant by shortening the title link of a post. For example we make the postings under the heading 'Make a slug on Codeigniter'. In the title the post URL length must also like 'http://www.codexlist.net/2016/02/make-a-slug-on-codeigniter'. But if we make use of the functions of the slug that had been long URLs can be such a short 'http://www.codexlist.net/2016/02/slug'. So the function of the slug in the wordpress.com is a url shortening for the post title too long, let the url we can easily on the detection by search engines such as google, yahoo, bing and others.
To make slug is actually very easy. In principle the slug will replace the ID. So first we need to add a slug column in the table of our news. Then create a function that will convert slug headlines into a slug. To make the functions of the slug, make a file slug_helper.php in the application folder/helper. The following is the content of the slug_helper.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); function slug($text) { // replace non letter or digits by - $text = preg_replace('~[^\\pL\d]+~u', '-', $text); // trim $text = trim($text, '-'); // transliterate $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); // lowercase $text = strtolower($text); // remove unwanted characters $text = preg_replace('~[^-\w]+~', '', $text); if (empty($text)) { return 'n-a'; } return $text; }
How to call him very easily. You simply load the slug helper in config/autoload.php
$autoload['helper'] = array('slug');
After you load the helper, you can directly use it like the following example.
public function create_action() { $this->_rules(); if ($this->form_validation->run() == FALSE) { $this->create(); } else { $data = array( 'news_title' => $this->input->post('news_title',TRUE), 'news_content' => $this->input->post('news_content',TRUE), 'slug_news' => slug($this->input->post('news_title',TRUE)), ); $this->news_model->insert($data); $this->session->set_flashdata('message', 'Create Record Success'); redirect(site_url('admin/berita')); } }
In the above example, we add a slug before saving function to slug_news clogs. To form need not be shown because slug are taken directly from the input news_title. so we add the title of the news 'this is the news title' then the slug will be a ' this-is-news-title'.
To prevent the same slug, you need to set the column being unique or you can perform the validation. For example for an existing slug can be added behind him increments.
So short way makes the slug that will improve your website SEO score. May be useful. If you have any questions please write in the comments field is Yes. Thank you.