4 Best CakePHP Behaviors

#2 Sluggable

When I was a kid the local arcade in Lincoln, NE was called “Sluggos”, it always smelled like urine and cigarettes. This behavior though most definitely DOES NOT smell like urine.  The Sluggable Behavior smells sweetly of SEO and flowers.  It’s extremely easy to use and will instantly make your application a success.

I’ll loosely borrow some of the code from the Bakery article but it couldn’t be simpler to understand.

app/models/post.php

class Post extends AppModel {
  var $name = 'Post';
  var $actsAs = array('Sluggable'); 
}

and ka-blamo! your “Post.title” field is sluggified (all the spaces and crud are replaced with hyphens) and automatically put in your “Post.slug” field.  then it’s just a matter tweaking your view() function in the Posts Controller to use the findBySlug($slug) method.  that’s all covered in the aforementioned Bakery Article.

#1 MeioUpload

By far, the newest kid on the block but this guy has blown me away.  I’ve already used it in a live CMS and I plan to use it for every site from here on out.  It’s brilliant.  I can’t stop my excitement for it.  Let me pontificate on it.

I’ve used the various File Handler models and components but it was always ugly and left me feeling dirty and unconventional inside.  The MeioUpload Behavior just makes sense.  It aids the form helper so un-obtrusively that it feels like it belongs in the core.

Follow my quick example but you’ll have to read the tutorial for a more detailed explanation.  Let’s say you have a catalog of products which have 1 image.

app/models/product.php

class Product extends AppModel {
  var $name = 'Product';
  var $actsAs = array('MeioUpload' => array(
    'image' => array('dir' =>"files{DS}uploads{DS}{model}")
// you're going to need a "Product.dir" and a "Product.image" field in the database
//  as well as create the "app/webroot/files/uploads" folder and set the necessary write permissions.
     )
  ); 
}              
There’s more customization to what can be done, but you are pretty much finished.  Now in your add/edit sections you just simply need to set the form helper to handle files:
app/views/products/add.php
<?php echo $form->create('Product', array('type'=>'file'));
  echo $form->input('title');
// ... whatever other information you need ...
  echo $form->input('image', array('type'=>'file'));

In conclusion

And that ladies and gentlemen [insert ladies in webdev joke here] is how you code up a behavior*.  I think behaviors are one of the sweet things about CakePHP that help de-muddle code and simplify the development process.

I look forward to future behaviors and opportunities to use them.  Especially the Chartable Vendor/Helper/Behavior because I love charts and graphs.  

I would like to learn the ACL behavior because I’ve tried and tried and ACO/ARO ACL doesn’t make sense.  If you have expertise, please shoot me an email or a comment. It confuses me.  I’d love to write a tutorial for dummies.

* For readers in the UK, I have been talking about “Behaviours” this whole time.  Just swap every mention of “Behavior” with “Behaviour” and it should all make perfect sense.

 

 

 

Pages: 1 2

Whachu got to say, Boss?