├── Slug.php └── readme.md /Slug.php: -------------------------------------------------------------------------------- 1 | set_config($config); 79 | log_message('debug', 'Slug Class Initialized'); 80 | } 81 | 82 | // ------------------------------------------------------------------------ 83 | 84 | /** 85 | * Manually Set Config 86 | * 87 | * Pass an array of config vars to override previous setup 88 | * 89 | * @param array 90 | * @return void 91 | */ 92 | public function set_config($config = array()) 93 | { 94 | if ( ! empty($config)) 95 | { 96 | foreach ($config as $key => $value) 97 | { 98 | $this->{$key} = $value; 99 | } 100 | } 101 | } 102 | 103 | // ------------------------------------------------------------------------ 104 | 105 | /** 106 | * Create a uri string 107 | * 108 | * This wraps into the _check_uri method to take a character 109 | * string and convert into ascii characters. 110 | * 111 | * @param mixed (string or array) 112 | * @param int 113 | * @uses Slug::_check_uri() 114 | * @uses Slug::create_slug() 115 | * @return string 116 | */ 117 | public function create_uri($data = '', $id = '') 118 | { 119 | if (empty($data)) 120 | { 121 | return FALSE; 122 | } 123 | 124 | if (is_array($data)) 125 | { 126 | if ( ! empty($data[$this->field])) 127 | { 128 | return $this->_check_uri($this->create_slug($data[$this->field]), $id); 129 | } 130 | elseif ( ! empty($data[$this->title])) 131 | { 132 | return $this->_check_uri($this->create_slug($data[$this->title]), $id); 133 | } 134 | } 135 | elseif (is_string($data)) 136 | { 137 | return $this->_check_uri($this->create_slug($data), $id); 138 | } 139 | 140 | return FALSE; 141 | } 142 | 143 | // ------------------------------------------------------------------------ 144 | 145 | /** 146 | * Create Slug 147 | * 148 | * Returns a string with all spaces converted to underscores (by default), accented 149 | * characters converted to non-accented characters, and non word characters removed. 150 | * 151 | * @param string $string the string you want to slug 152 | * @param string $replacement will replace keys in map 153 | * @return string 154 | */ 155 | public function create_slug($string) 156 | { 157 | $CI =& get_instance(); 158 | $CI->load->helper(array('url', 'text', 'string')); 159 | $string = strtolower(url_title(convert_accented_characters($string), $this->replacement)); 160 | return reduce_multiples($string, $this->_get_replacement(), TRUE); 161 | } 162 | 163 | // ------------------------------------------------------------------------ 164 | 165 | /** 166 | * Check URI 167 | * 168 | * Checks other items for the same uri and if something else has it 169 | * change the name to "name-1". 170 | * 171 | * @param string $uri 172 | * @param int $id 173 | * @param int $count 174 | * @return string 175 | */ 176 | private function _check_uri($uri, $id = FALSE, $count = 0) 177 | { 178 | $CI =& get_instance(); 179 | $new_uri = ($count > 0) ? $uri.$this->_get_replacement().$count : $uri; 180 | 181 | // Setup the query 182 | $CI->db->select($this->field)->where($this->field, $new_uri); 183 | 184 | if ($id) 185 | { 186 | $CI->db->where($this->id.' !=', $id); 187 | } 188 | 189 | if ($CI->db->count_all_results($this->table) > 0) 190 | { 191 | return $this->_check_uri($uri, $id, ++$count); 192 | } 193 | else 194 | { 195 | return $new_uri; 196 | } 197 | } 198 | 199 | // ------------------------------------------------------------------------ 200 | 201 | /** 202 | * Get the replacement type 203 | * 204 | * Either a dash or underscore generated off the term. 205 | * 206 | * @return string 207 | */ 208 | private function _get_replacement() 209 | { 210 | return ($this->replacement === 'dash') ? '-' : '_'; 211 | } 212 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # CodeIgniter Slug Library 2 | 3 | This library is designed to help you generate friendly uri strings for your content stored in the database. 4 | 5 | For example if you have a blog post table then you would want uri strings such as: mysite.com/post/my-post-title 6 | 7 | The problem with this is each post needs a unique uri string and this library is designed to handle that for you. 8 | 9 | So if you add another with the same uri or title it would convert it to: mysite.com/post/my-post-title-2 10 | 11 | # Requirements 12 | 13 | * CodeIgniter 14 | * Some form of database supported by active record 15 | 16 | # Usage 17 | 18 | ## Here is an example setup: 19 | 20 | Please note that these fields map to your database table fields. 21 | 22 | $config = array( 23 | 'field' => 'uri', 24 | 'title' => 'title', 25 | 'table' => 'mytable', 26 | 'id' => 'id', 27 | ); 28 | $this->load->library('slug', $config); 29 | 30 | ## Adding and Editing Records: 31 | 32 | When creating a uri for adding to the database you will use something like this: 33 | 34 | $data = array( 35 | 'title' => 'My Test', 36 | ); 37 | $data['uri'] = $this->slug->create_uri($data); 38 | $this->db->insert('mytable, $data); 39 | 40 | Then for editing: (Notice the create_uri uses the second param to compare against other fields). 41 | 42 | $id = 1; 43 | $data = array( 44 | 'title' => 'My Test', 45 | ); 46 | $data['uri'] = $this->slug->create_uri($data, $id); 47 | $this->db->where('id', $id); 48 | $this->db->update('mytable', $data); 49 | 50 | ## Methods 51 | 52 | ### __construct($config = array()) 53 | 54 | Setup the library with your config options. 55 | 56 | ```php 57 | $config = array( 58 | 'table' => 'mytable', 59 | 'id' => 'id', 60 | 'field' => 'uri', 61 | 'title' => 'title', 62 | 'replacement' => 'dash' // Either dash or underscore 63 | ); 64 | $this->load->library('slug', $config); 65 | ``` 66 | 67 | ### set_config($config = array()) 68 | 69 | Pass an array of config vars that will override setup 70 | 71 | **Paramaters** 72 | 73 | * $config - (required) - Array of config options 74 | 75 | ```php 76 | $config = array( 77 | 'table' => 'mytable', 78 | 'id' => 'id', 79 | 'field' => 'uri', 80 | 'title' => 'title', 81 | 'replacement' => 'dash' // Either dash or underscore 82 | ); 83 | $this->slug->set_config($config); 84 | ``` 85 | ### create_uri($data = '', $id = '') 86 | 87 | Creates the actual uri string and in the background validates against the table to ensure it is unique. 88 | 89 | **Paramaters** 90 | 91 | * $data - (requied) Array of data 92 | * $id - (optional) Id of current record 93 | 94 | ```php 95 | $data = array( 96 | 'title' => 'My Test', 97 | ); 98 | $this->slug->create_uri($data) 99 | ``` 100 | 101 | ```php 102 | $data = array( 103 | 'title' => 'My Test', 104 | ); 105 | $this->slug->create_uri($data, 1) 106 | ``` 107 | 108 | This returns a string of the new uri. 109 | --------------------------------------------------------------------------------