sylwesterkowal/plugin.cakephp-slug

CakePHP Plugin for Slugging

dev-master 2016-09-11 21:44 UTC

This package is not auto-updated.

Last update: 2024-12-14 14:55:20 UTC


README

Plugin CakePHP to generate friendly URLs. Based on the database allows you to generate simple URL for any data model:
Product Link: http://domain.com/slugged-product-name
Category Link: http://domain.com/slugged-category-name

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require sylwesterkowal/plugin.cakephp-slug

Install Table

cake Migrations migrate -p Slug

Configurations

Configuring the plugin is to add the relationship between table Slug and tables in which to use the generated URL slug.

ModelTable

For example in plugin "Product" for table "ProductsTable.php" add

    public function initialize(array $config)
    {

        ...
        ...

        $this->hasOne('Slug.Slugs', [
            'className' => 'Slugs',
            'foreignKey' => 'pass',
            'conditions' => [
                'plugin' => 'Product',  // Set plugin name
                'controller' => 'Products', // Set controller name
                'action' => 'view'  // Set action name
            ]
        ]);


        $this->addBehavior('Slug.Sluggable', [
            'field' => 'name', // enter the field, which will be generated slug
            'plugin' => 'Product', // set plugin name
            'controller' => 'Products', // set controller name
            'action' => 'view', // set action name
            'pass' => 'id',  // set the field, which will by ID Key
        ]);

    }

Routes configuration

    $routes->connect(
        '/:slug',
        [],
        ['routeClass' => 'Slug.SlugRoute']
    );

Controller

public $helpers = ['Slug.Slug'];

View

In view use SlugHelper. Parameters are require for standard Html->link url when Slug Plugin for some reason will by offline.

echo $this->Slug->link(
    $productEntity,  // Model Entity
    $productEntity->name, // Anchor text
    [ // parameters
        'controller'=>'Products',
        'action'=>'view', $productEntity->id,
        'plugin'=>'Product'
    ]
);