Return to Snippet

Revision: 60134
at October 23, 2012 01:12 by artaserse


Initial Code
/**
 * genera uno slug unico per le ricerche e i fancy url
 * @param $string {string} stringa da trasformare in slug
 * @return $slug {string} uno slug valido (yyy-yyyyyy-yyy), appende un numero alla fine per renderlo unico
 */
 	public function __to_slug($string) {		
		$slug = strtolower(Inflector::slug($string, '-'));
		$count = $this->find( 'count', array(
			'conditions' => array(
				$this->alias . ".slug REGEXP" => "^($slug)(-\d+)?"
			)
		));
		// genero un url  nome-slug-1 -- nome-slug-2 
		if($count > 0) return $slug . "-" . $count;
		else return $slug;
	}

Initial URL

                                

Initial Description
this method generates and returns a unique slug, given a string.

Initial Title
CakePHP - generate unique slug

Initial Tags
cakephp

Initial Language
PHP