- cakephp 5.x.x
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require atelfoto/sluggable
- or add it in the composer.json
"require": {
"atelfoto/sluggable": "1.*"
},
- And in a Table in
initialize();
add this line:
//src/Model/Table/model.php
$this->addBehavior('Sluggable.Sluggable');
- Field
- Default: name
- The field to slug
$this->addBehavior('Sluggable.Sluggable', [ 'field' => 'your-field' ] );
- Slug
- Default: slug
- The slug field name in your database:
$this->addBehavior('Sluggable.Sluggable', [ 'field' => 'your-field', 'slug' => 'your-slug' ] );
- Replacement
-
Default:
-
-
The replacement characters used to replace space:
$this->addBehavior('Sluggable.Sluggable', [ 'replacement' => '_' ]);
-
-
To use in a finder:
public function view($value = null) { $query = $this->Models->findBySlug($value)->firstOrFail(); $this->set(compact('query')); }
-
In the Route
//config/routes $routes->connect( '/the title/:slug', ['controller' => 'YourController', "action" => "view"], [ "_name" => "the title", "pass" => ['slug'], ] );