How To Manually Install Cocur Slugify
Published January 8, 2026.
Slugify by Florian Eckerstorfer (cocur/slugify on GitHub) is a PHP class that "converts a string into a slug", which I implemented in my static blog generator.
Slugification is taking a string like "My Clever Post Title" and converting it to "my-clever-post-title", which is quite useful for making filenames that are URL & SEO friendly.
I was pleased to read that Slugify requires "no external dependencies" -- unlike many single-purpose components that seem to need an entire other framework in order to function!
However, I was less pleased to see that the installation instructions were essentially "you can install Slugify through Composer".
As far as I'm concerned, the Composer and his entire orchestra can exit stage left. 👉 When I use a library in my project, I want to place it in a suitable location, and include it where needed: plain and simple.
As it turns out, manual inclusion of Slugify is easy; you just need two classes and their interfaces:
require_once 'slugify/RuleProvider/RuleProviderInterface.php';
require_once 'slugify/RuleProvider/DefaultRuleProvider.php';
require_once 'slugify/SlugifyInterface.php';
require_once 'slugify/Slugify.php';
$slugify = new Cocur\Slugify\Slugify();
The above example assumes you have renamed the original src folder to slugify, for clarity.
Of course, doing four includes is less than ideal if you need to do it in more than one place.
A Better Method
For a more elegant solution, make a new file called slugify.inc.php one directory level above your slugify folder, and copy the following code into it:
<?php
require_once __DIR__ . '/slugify/RuleProvider/RuleProviderInterface.php';
require_once __DIR__ . '/slugify/RuleProvider/DefaultRuleProvider.php';
require_once __DIR__ . '/slugify/SlugifyInterface.php';
require_once __DIR__ . '/slugify/Slugify.php';
?>
In PHP, __DIR__ is a magic constant that returns the directory of the file that it is in, even if that file is being included in another one. Hence, slugify.inc.php will always successfully find all the files, regardless of where the including script is located.
And then you're off to the races:
require_once 'slugify.inc.php';
$slugify = new Cocur\Slugify\Slugify();
(Or off-to-the-races, as the case may be!)
N.B.- DefaultRuleProvider.php weighs in at over 200 KB! Especially for dynamic websites in shared hosting environments, I advise including Slugify only where and as needed.
Adding Rules
As a bonus, an appealing feature of Slugify is its addRule() method, which conveniently allows you to add your own rules without having to extend the class or hack on it.
For example: default behaviour is to eliminate ampersands (&), and replace apostrophes (') with separators, so the string "Why You Can't & Won't" becomes "why-you-can-t-won-t".
I changed this as follows:
$slugify->addRule('&', 'and'); // replace ampersands with "and"
$slugify->addRule("'", ""); // eliminate apostrophes
Now the string "Why You Can't & Won't" comes out as "why-you-cant-and-wont", which I found more sensible grammatically and for my own use.
At the end of the day, that's one less wheel I needed to re-invent.
Happy slugifying!