Created
January 5, 2013 06:17
-
-
Save harshamv/4460103 to your computer and use it in GitHub Desktop.
Generate URL Slug for CakePHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Creates a URL Slug | |
function _url_slug($string) { | |
$string = strtolower(trim($string)); | |
$string = preg_replace('/[^a-z0-9-]/', '-', $string); | |
$string = preg_replace('/-+/', "-", $string); | |
return $string; | |
} | |
// Gets a Unique slug string by checking in the database | |
function _getSlug($name, $area, $model) { | |
$this->$model->Area->recursive = -1; | |
$areaSlug = $this->$model->Area->read('slug', $area); | |
$name = $name . ' ' . $areaSlug['Area']['slug']; | |
$slug = $slug_copy = $this->_url_slug($name); | |
$i = 1; | |
while ($this->$model->find('count', array('conditions' => array($model . '.slug' => | |
$slug))) == true) { | |
$slug = $slug_copy . '-' . $i; | |
$i++; | |
} | |
return $slug; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment