A simple Cake3 plugin to slug fields and find records by slug.
- CakePHP 3.X
Run : composer require xety/cake3-sluggable:1.*
Or add it in your composer.json
:
"require": {
"xety/cake3-sluggable": "1.*"
},
In your config/bootstrap.php
add :
Plugin::load('Xety/Cake3Sluggable');
In your model initialize()
:
$this->addBehavior('Xety/Cake3Sluggable.Sluggable');
To use the custom finder :
$this->Users
->find('slug', [
'slug' => 'your-slug-here',
'slugField' => 'Users.slug'
]);
-
Default :
title
The field to slug :
$this->addBehavior('Xety/Cake3Sluggable.Sluggable', [ 'field' => 'username' ] );
-
Default :
slug
The slug field name in your database :
$this->addBehavior('Xety/Cake3Sluggable.Sluggable', [ 'field' => 'username', 'slug' => 'slug_username' ] );
-
Default :
-
The replacement characters used to replace space etc :
$this->addBehavior('Xety/Cake3Sluggable.Sluggable', [ 'replacement' => '_' ] );