Reputation: 28843
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
Upvotes: 1
Views: 895
Reputation: 5266
CakePHP 5:
Cake\Utility\Text::slug()
muffin/slug
pluginUpvotes: -1
Reputation: 14808
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.
Upvotes: 1
Reputation: 2872
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
Upvotes: 0
Reputation: 5481
you can do it in either the controller or the model, when you save the post.
Upvotes: 0