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
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.
you can do it in either the controller or the model, when you save the post.
I wouldn't use slugs (technically) for pointing to content. You're probably better of using the ID and include the slug in the URL's for SEO purposes only. This can easily be achieved when needed using the Inflector::slug() method build into Cake.
See also my post here: CakePHP: Use post title as the slug for view method
精彩评论