PHPackages                             simtecsystem/cakephp-sluggable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. simtecsystem/cakephp-sluggable

ActiveCakephp-plugin

simtecsystem/cakephp-sluggable
==============================

Plugin for CakePHP 3.x that enables automatic, configurable slugging of database fields

1.0.4(6y ago)014MITPHPPHP &gt;=5.4

Since Aug 13Pushed 6y agoCompare

[ Source](https://github.com/simtecsystem/cakephp-sluggable)[ Packagist](https://packagist.org/packages/simtecsystem/cakephp-sluggable)[ Docs](https://github.com/simtecsystem/cakephp-sluggable)[ RSS](/packages/simtecsystem-cakephp-sluggable/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

cakephp-sluggable
=================

[](#cakephp-sluggable)

Plugin for CakePHP 3.x that enables automatic, configurable slugging of database fields

***WHY?***

Because slugs are great for human-readable yet seo-friendly page titles, urls, image urls, etc! They're pretty much the standard nowadays and CakePHP makes it super easy to give your app the power to create them for you.

***HOW?***

Just add the `Sluggable.Sluggable` behaviour to any model whose field(s) you need to slug. See the usage section for customization.

Requirements
------------

[](#requirements)

- PHP 5.4+
- [CakePHP 3.x](http://cakephp.org)

TOC
---

[](#toc)

1. Plugin Installation
2. Usage
3. Examples
4. Contributing

### Plugin Installation

[](#plugin-installation)

1. Composer Install
2. Manual Install
3. Loading the plugin in your app
4. Setting up the namespace / autoloader

#### Composer Install

[](#composer-install)

This plugin is on Packagist which means it can be easily installed with Composer.

```
composer require simtecsystem/cakephp-sluggable

```

Then simply load the plugin normally in your `config/bootstrap.php` file

```
# in ../config/bootstrap.php - right after Plugin::load('Migrations') is fine!
Plugin::load('Sluggable');
```

#### Manual Install

[](#manual-install)

You can also manually load this plugin in your App

##### loading the plugin in your app

[](#loading-the-plugin-in-your-app)

Add the source code in this project into `plugins/Sluggable`

Then configure your App to actually load this plugin

```
# in ../config/bootstrap.php
Plugin::load('Sluggable');
```

##### setting up the namespace / autoloader

[](#setting-up-the-namespace--autoloader)

Tell the autoloader where to find your namespace in your `composer.json` file

```
	(..)
    "autoload": {
        "psr-4": {
           (..)
            "Sluggable\\": "./plugins/Sluggable/src"
        }
    },
    (..)
```

Then you need to issue the following command on the commandline

```
	php composer.phar dumpautoload

```

If you are unable to get composer autoloading to work, add `'autoload' => true` line in your `bootstrap.php` `Plugin::load(..)` command (see loading section)

Slug Behavior
-------------

[](#slug-behavior)

The sluggable behavior is extremely easy to implement, simply add it, like any other behavior, to your `Table`

```
class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);

		$this->addBehavior('Sluggable.Sluggable');
	}
}
```

By default the plugin will automatically generate a slug based on `name`, will store it in a column called `slug` and will use a dash `-` replacement, and will NOT automatically overwrite the slug field whenever `name` changes.

All of these settings are, of course, configurable.

- `pattern`
    - `:name` *(default)*
    - a `\Cake\Utility\Text::insert()`-friendly tokenized string. any of the entity fields are valid options
- `field`
    - `slug` *(default)*
    - field in the entity to store the slug
- `replacement`
    - `-` *(default)*
    - string used to replace spaces when building the slug
- `overwrite`
    - `false` *(default)*
    - `true`, if the slug should ALWAYS be re-generated on save. `false`, to generate once

#### Examples

[](#examples)

Generate a slug based on the `title` field instead of `name`

```
class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);

		$this->addBehavior('Sluggable.Sluggable', [
			'pattern' => ':title',
		]);
	}
}
```

Generate a slug based on `id` **and** `title`

```
class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);

		$this->addBehavior('Sluggable.Sluggable', [
			'pattern' => ':id-:title',
		]);
	}
}
```

Generate a slug based on the latest version of the `title` (always)

```
class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);

		$this->addBehavior('Sluggable.Sluggable', [
			'pattern' => ':title',
			'overwrite' => true,
		]);
	}
}
```

Generate a slug normally, but store it in the `foo` column

```
class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);

		$this->addBehavior('Sluggable.Sluggable', [
			'field' => 'foo',
		]);
	}
}
```

Generate a slug using `.` dots instead of `-` dashes

```
class PostsTable extends Table
{
	public function initialize(array $options)
	{
		parent::initialize($options);

		$this->addBehavior('Sluggable.Sluggable', [
			'replacement' => '.',
		]);
	}
}
```

Slug Utility
------------

[](#slug-utility)

The Sluggable Plugin adds a Utility class `Slug` that can be called statically. This is the function used by the Behavior to actually generate the slug.

It is capable of handling a string, array, or entity in conjunction with a simple string or `Text::insert`-friendly pattern.

To use the Utility, simply add the following to your class header

```
	use Sluggable\Utility\Slug;
```

The Utility provides the following function

```
 /**
     * Turns a string (and optionally a dynamic, data-injected string) into a slugged value
     * @param $pattern string a simple string (e.g. 'slug me')
     * 						  or Text::insert-friendly string (e.g. ':id-:name')
     * @param $data mixed an Array or Entity of data to Text::insert inject into $pattern
     * @param $replacement string the character to replace non-slug-friendly characters with (default '-')
     * @return string the slugged string
     */
    Slug::generate($pattern, $data = [], $replacement = '-');
```

#### Examples

[](#examples-1)

```
	use Sluggable\Utility\Slug;

	echo Slug::generate('slug me');
	# 'slug-me'

	echo Slug::generate('SLUG(!@#(ME');
    # 'slug-me'

    echo Slug::generate('a really long slug that i just made');
    # 'a-really-long-slug-that-i-just-made'
```

To Text::insert via an `array`...

```
	$data = [
		'id' => 123,
		'name' => 'abc',
		'description' => 'Hello, World!',
	];

	$slug = Slug::generate(':id-:name', $data);
	# '123-abc'

	$slug = Slug::generate(':description', $data);
	# 'hello-world'
```

To Text::insert via `Entity` properties...

```
	$data = new Entity([
		'id' => 123,
		'name' => 'abc',
		'description' => 'Hello, World!',
	]);

	$slug = Slug::generate(':id-:name', $data);
	# '123-abc'

	$slug = Slug::generate(':description', $data);
	# 'hello-world'
```

Contributing
============

[](#contributing)

If you'd like to contribute, please submit a PR with your changes!

Requests will be accepted more readily if they come complete with **TESTS** :D

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~348 days

Total

5

Last Release

2538d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0fa21215cfd3df464f376b331dd62e00a635bd7ed46e3567437ee480b676df61?d=identicon)[simtecsystem](/maintainers/simtecsystem)

---

Top Contributors

[![cwbit](https://avatars.githubusercontent.com/u/6501207?v=4)](https://github.com/cwbit "cwbit (6 commits)")[![kajko1973](https://avatars.githubusercontent.com/u/13366107?v=4)](https://github.com/kajko1973 "kajko1973 (2 commits)")

---

Tags

pluginslugcakephpsluggable

### Embed Badge

![Health badge](/badges/simtecsystem-cakephp-sluggable/health.svg)

```
[![Health](https://phpackages.com/badges/simtecsystem-cakephp-sluggable/health.svg)](https://phpackages.com/packages/simtecsystem-cakephp-sluggable)
```

###  Alternatives

[dereuromark/cakephp-tools

A CakePHP plugin containing lots of useful and reusable tools

338920.1k32](/packages/dereuromark-cakephp-tools)[dereuromark/cakephp-tinyauth

A CakePHP plugin to handle user authentication and authorization the easy way.

129228.6k10](/packages/dereuromark-cakephp-tinyauth)[dereuromark/cakephp-databaselog

A CakePHP plugin for storing and viewing application logs in the database

44165.0k2](/packages/dereuromark-cakephp-databaselog)[dereuromark/cakephp-setup

A CakePHP plugin containing lots of useful management tools

36162.8k2](/packages/dereuromark-cakephp-setup)[dereuromark/cakephp-dto

A CakePHP plugin for generating immutable Data Transfer Objects with full type safety

2988.9k3](/packages/dereuromark-cakephp-dto)[dereuromark/cakephp-translate

A CakePHP plugin for managing translations

1710.4k](/packages/dereuromark-cakephp-translate)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
