cakephp

CakePHP: Save inflected title into a slug column


I have a database table called posts that has 'id, title, slug and content'

I want to automatically fill the slug column with an inflected version of the title that has been entered when creating or editing the post.

So for example if I created a post called: "Welcome to my Blog" the following slug would be saved in the db for that post: "Welcome_to_my_Blog".

I presume this is something you would do in the controller?

Can anyone help? Thanks


Solution

  • I use the CakeDC Utils plugin, it has a "sluggable" behaviour and you just set the field name of the slug and it will save a slug for you automatically from the name field (you can specify another field if you need). Once you've dropped it into your plugins folder, here is my setup:

    public $actsAs = array(
        'Utils.Sluggable' => array(
            'label' => 'name',
            'method' => 'multibyteSlug',
            'separator' => '-'
        )
    );
    

    Make sure you have a slug field in your database.