To create a unique cakephp slug
Generate unique slugs in CakePHP
I
believe it was Wordpress who came up with the term "slug". It has
been adopted by many other systems though, and it basically means the
following: a unique identifier which can be used in a URL to create a
permalink to a page.
At
least, that's my interpretation of it. In this article I will show you how you
can generate a unique slug easily when working with the CakePHP Framework.
Since the slug
should be unique across records in the same database table, the best place to
store slug-generating functionality is in the AppModel, which is, strictly speaking, the
only business logic layer that may access the database.
If you haven't got
an AppModel created
yet, add one in /app/app_model.PHP. You may fill it with the following code:
1.
<?php
2.
class AppModel extends Model {
3.
4.
function createSlug ($string,
$id=null) {
5.
$slug = Inflector::slug
($string,'-');
6.
$slug = low ($slug);
7.
$i = 0;
8.
$params = array ();
9.
$params ['conditions']= array();
10.
$params
['conditions'][$this->name.'.slug']= $slug;
11.
if (!is_null($id)) {
12.
$params ['conditions']['not'] =
array($this->name.'.id'=>$id);
13.
}
14.
while (count($this->find
('all',$params))) {
15.
if (!preg_match ('/-{1}[0-9]+$/',
$slug )) {
16.
$slug .= '-' . ++$i;
17.
} else {
18.
$slug = preg_replace ('/[0-9]+$/',
++$i, $slug );
19.
}
20.
$params ['conditions'][$this->name
. '.slug']= $slug;
21.
}
22.
return $slug;
23.
}
24.
}
25.
?>
What this code
does, is providing a method, createSlug (), which can be accessed from all models in your
application. It'll normalize the string, make it URL-friendly and last but
not least, it makes it unique.
To demonstrate the
"unique" part, let's say we've got a record with the title "I
love CakePHP". The createSlug method will turn this into "i-love-cakePHP". Human friendly and search-engine
friendly.
What happens when I wish to create two more items in my database called "I love CakePHP"? The createSlug method will generate the following two slugs: i-love-cakePHP-1 and i-love-cakePHP-2.
What happens when I wish to create two more items in my database called "I love CakePHP"? The createSlug method will generate the following two slugs: i-love-cakePHP-1 and i-love-cakePHP-2.
This way users can
bookmark your URLs and always end up in the right place, even though the
titles of your records may be similar.
How to use?
It's simple,
really. Since the method is created in the AppModel base class, you can invoke it from every
model in your application. When saving a record, you can simply call...
1.
$slug = $this->createSlug ('my
title');
...from within your
models, or...
1.
$slug =
$this->MyModel->createSlug ('my title');
2. $this->request->data['model']['slug'] = slug;
...from within your
controllers, right before you insert new data.
Note that you have
to pass the id from the current record when you're modifying existing records,
so it can exclude that from its check, like this:
1.
$slug = $this->createSlug ('my
title', 10);
refer :
http://www.whatstyle.net/articles/52/generate_unique_slugs_in_cakephp
0 comments:
Post a Comment