Pada kesempatan kali ini saya akan menjelaskan cara membuat slug di CodeIgniter. Sebelumnya perlu teman-teman ketahui, Slug (siput) adalah url yang dihasilkan dari judul atau title di posting artikel website teman-teman. Fungsi dari slug tersebut adalah perwakilan dari judul posting artikel di url browser supaya url tersebut menjadi friendly dan tentunya sangat di sukai oleh mesin pencari seperti google. Dengan mengganti karakter-karakter seperti spasi menjadi (-). Seperti contoh, misal saya memiliki url website seperti berikut ( http://dumetschool.com/blog.php?id=22 ) , tentu saja url tersebut kurang friendly dan sangat beresiko untuk keamanan website yang teman-teman buat. Maka dari itu teman-teman di haruskan membuat slug yang gunanya untuk menampung link url berdasarkan judul dari artikel tersebut. Pada penjelasan di sini teman-teman bisa ikuti saya membuat slug di CodeIgniter.
Berikut Caranya :
- Sebelumnya saya asumsikan bahwa saja teman-teman sudah melakukan instalasi CodeIgniter sebagai syarat utamanya.
- Buat satu buah library CodeIgniter dengan nama Slug.php, salin kode seperti di bawah ini :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter Slug Library */ // ------------------------------------------------------------------------ /** * Slug Library * @subpackage Libraries */ class Slug { /** * The name of the table * * @var string */ public $table = ''; /** * The primary id field in the table * * @var string */ public $id = 'id'; /** * The URI Field in the table * * @var string */ public $field = 'uri'; /** * The title field in the table * * @var string */ public $title = 'title'; /** * The replacement (Either underscore or dash) */ public $replacement = 'dash'; // ------------------------------------------------------------------------ /** * Setup all vars * */ public function __construct($config = array()) { $this->set_config($config); log_message('debug', 'Slug Class Initialized'); } // ------------------------------------------------------------------------ /** * Manually Set Config * * Pass an array of config vars to override previous setup */ public function set_config($config = array()) { if ( ! empty($config)) { foreach ($config as $key => $value) { $this->{$key} = $value; } } } // ------------------------------------------------------------------------ /** * Create a uri string * * This wraps into the _check_uri method to take a character * string and convert into ascii characters. */ public function create_uri($data = '', $id = '') { if (empty($data)) { return FALSE; } if (is_array($data)) { if ( ! empty($data[$this->field])) { return $this->_check_uri($this->create_slug($data[$this->field]), $id); } elseif ( ! empty($data[$this->title])) { return $this->_check_uri($this->create_slug($data[$this->title]), $id); } } elseif (is_string($data)) { return $this->_check_uri($this->create_slug($data), $id); } return FALSE; } // ------------------------------------------------------------------------ /** * Create Slug * * Returns a string with all spaces converted to underscores (by default), accented * characters converted to non-accented characters, and non word characters removed. */ public function create_slug($string) { $CI =& get_instance(); $CI->load->helper(array('url', 'text', 'string')); $string = strtolower(url_title(convert_accented_characters($string), $this->replacement)); return reduce_multiples($string, $this->_get_replacement(), TRUE); } // ------------------------------------------------------------------------ /** * Check URI * * Checks other items for the same uri and if something else has it * change the name to "name-1". */ private function _check_uri($uri, $id = FALSE, $count = 0) { $CI =& get_instance(); $new_uri = ($count > 0) ? $uri.$this->_get_replacement().$count : $uri; // Setup the query $CI->db->select($this->field)->where($this->field, $new_uri); if ($id) { $CI->db->where($this->id.' !=', $id); } if ($CI->db->count_all_results($this->table) > 0) { return $this->_check_uri($uri, $id, ++$count); } else { return $new_uri; } } // ------------------------------------------------------------------------ /** * Get the replacement type * * Either a dash or underscore generated off the term. * * @return string */ private function _get_replacement() { return ($this->replacement === 'dash') ? '-' : '_'; } } |
Untuk penjelasan cara membuat slug di CodeIgniter, akan saya sambung pada sesi berikutnya, semoga bermanfaat.