gandh1pl / cakephp-slugs
This extension contains helper classes to easily implement dynamical slugs (from database) in CakePHP 3 project.
dev-master
2017-02-22 03:15 UTC
Requires
- cakephp/cakephp: >=3.1.0 <4.0
This package is auto-updated.
Last update: 2025-03-14 19:44:39 UTC
README
This extension provides helper classes to easily implement dynamical slugs in CakePHP 3 Project.
Dynamical slugs means slugs loaded from database.
Configuration
- In your
config/routes.php
, instead offallbacks
rules use that rule:
$routes->connect('/:controller/:action/*', ['action' => 'index'], ['routeClass' => 'gandh1pl\Cake\Slugs\Routing\Route\SlugRoute']);
- In
App\Model\Table
namespace create fileRoutesTable.php
and paste code below:
namespace App\Model\Table;
/**
* Class RoutesTable
*
* @package App\Model\Table
*/
class RoutesTable extends \gandh1pl\Cake\Slugs\Model\Table\RoutesTable
{
/**
* Cache engine name. Set to false if you don't wanna to use query's caching.
* @var string|false
*/
protected $_cacheEngine = 'slugs';
/**
* Name of route's table. Defaults to `routes`, but you can change it.
* @var string
*/
protected $tableName = 'routes';
/**
* Preferred hydrate option when making query to database. Defaults to false.
* @var bool
*/
protected $defaultHydrate = false;
}
- In
App\Model\Entity
namespace create fileRoute.php
and paste code below:
namespace App\Model\Entity;
class Route extends \gandh1pl\Cake\Slugs\Model\Entity { }
- In table class on which you want to use slugs (for example ArticleTable.php), in
initialize()
method:
$this->addBehavior('Slug', [
'className' => '\gandh1pl\Cake\Slugs\Model\Behavior\SlugBehavior',
'urlTemplate' => '/article/view/:id',
'urlParameters' => ['id' => 'entity_id'],
'attributesToGenerateFrom' => ['category.name', 'title', 'id'],
// 'minimumAttributesToGenerateFrom' => 1,
// 'generateSlugOnInsert' => true,
// 'routeRelationBindingKey' => 'route_id',
]);