number9,

Have you seen the sluggable behavior - 
http://bakery.cakephp.org/articles/view/sluggable-behavior?

Or better yet, read the section 'Stupid Easy URL Slugs' on page 42, or
mcurry's free CakePHP E-book -
Super Awesome Advanced CakePHP Tips
http://www.pseudocoder.com/archives/2009/05/13/free-cakephp-e-book-super-awesome-advanced-cakephp-tips/

-teh



On May 23, 11:07 am, number9 <[email protected]> wrote:
> I pieced together this code from a few tutorials and was very happy
> with it until I realised it has a bit of a flaw! The code is placed in
> the app model, and automatically adds a .html extension, and turns the
> name field into a suitable URL. It also checks whether the slug is
> unique, if it isn't it will add a number before the slug so that it is
> unique. (If there is already a number there, then it will keep looping
> until it finds a unique number). All of this is done automatically,
> with no code in the controller/view as long as there is a "slug" and a
> "name" field.
>
> So what's the problem? Problem is, when something is updated (i.e.
> edit action), it sees the slug is not unique and adds a number... I
> have solved this by specifying the fields to save in each edit
> controller, but thought there must be an easier way to stop the code
> running for every edit function? The thing I like about this is that
> it requires no real setting up in the controllers. I know what I want
> to do in pseudocode in terms of an if/else statement somewhere, but
> cannot see how I could add it?
>
> Here is the code anyway:
>
> <?php
>
> class AppModel extends Model
> {
>
>         function beforeValidate() {
>             $modelName = $this->name; // Put the model's name in an easier to
> read variable
>
>             // If there's a slug field present, auto-populate it
>             if ($this->hasField('slug')) {
>               if (isset($this->data[$modelName]['name'])) {
>                 // If the model has a name, use that to make its slug
>                 $this->data[$modelName]['slug'] = 
> $this->createSlug($this->data[$modelName]['name']);
>
>               } else {
>                 // Otherwise, use anything that'll give it a unique
> identifier as it's easier for absolutely everything to use slugs -
> less exceptions to the rules make for simplicity.  I'm going to
> serialise the data (turn the array into a string) and make an MD5 sum
> of it (turn that into a few random-looking hex digits).  Hopefully
> this should be unique.  I'd be worried if it wasn't.
>                 $this->data[$modelName]['slug'] = md5(serialize($this->data
> [$modelName]));
>               }
>             }
>           return TRUE; // Go ahead and validate the data
>         }
>
>         function createSlug ($string, $id=null) {
>         // Turn names such as "It's wrinkled!" into slugs such as "its-
> wrinkled"
>         $slug = strtolower($string); // Make the string lowercase
>         $slug = str_replace('\'', '', $slug); // Take out apostraphes.
> CakePHP takes care of escaping strings before putting them into SQL
> queries. This is purely for aesthetic purposes - changing "that-s-
> cool" into "thats-cool"
>         $slug = ereg_replace('[^[:alnum:]]+', '-', $slug); // Turn any group
> of non-alphanumerics into a single hyphen
>         $slug = trim($slug, '-'); // Remove unnecessary hyphens from
> beginning and end
>         $slug = $slug.".html"; // add html extension so pages appear static.
>
>         // Now loop through slugs, if there is an identical one - add a
> number at the beginning.
>         $i = 0;
>         $params = array ();
>         $params ['conditions']= array();
>         $params ['conditions'][$this->name.'.slug']= $slug;
>                 if (!is_null($id)) {
>                 $params ['conditions']['not'] = array($this->name.'.id'=>$id);
>                 }
>                 while (count($this->find ('all',$params))) {
>                 if (!preg_match ('/-{1}[0-9]+$/', $slug )) {
>                 // trim .html extension so that number can be added:
>                 $slug = substr_replace($slug ,"",-5);
>                 // now reattach .html after number
>                 $slug .= '-' . ++$i.'.html';
>                 } else {
>                 $slug = preg_replace ('/[0-9]+$/', ++$i, $slug );
>                 }
>                 $params ['conditions'][$this->name . '.slug']= $slug;
>                 }
>         return $slug;
>         }
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to