PHPackages                             sawastacks/ci4-slugify - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. sawastacks/ci4-slugify

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sawastacks/ci4-slugify
======================

Simple Slugify Library for Codeigniter

v2.1.0(1y ago)72661[1 issues](https://github.com/sawastacks/ci4-slugify/issues)MITPHPPHP ^7.2 || ^8.0

Since Aug 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/sawastacks/ci4-slugify)[ Packagist](https://packagist.org/packages/sawastacks/ci4-slugify)[ Docs](https://github.com/sawastacks/ci4-slugify)[ RSS](/packages/sawastacks-ci4-slugify/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/05f004561035d9bd707e4171f0bf0b5a16672b79526090ba6e206a64f2c2597e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73617761737461636b732f6369342d736c7567696679)](https://camo.githubusercontent.com/05f004561035d9bd707e4171f0bf0b5a16672b79526090ba6e206a64f2c2597e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73617761737461636b732f6369342d736c7567696679) [![GitHub code size in bytes](https://camo.githubusercontent.com/09b751380703aa271dd77e65cbeca993bc2a8df592cc9fb101a08a9eee930091/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f73617761737461636b732f6369342d736c7567696679)](https://camo.githubusercontent.com/09b751380703aa271dd77e65cbeca993bc2a8df592cc9fb101a08a9eee930091/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f73617761737461636b732f6369342d736c7567696679) [![Total Downloads](https://camo.githubusercontent.com/0df4f185e7e906ec50d14c9218b2b5045f03b9c1c5ac2db2c797b7f84b86dc64/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73617761737461636b732f6369342d736c7567696679)](https://packagist.org/packages/sawastacks/ci4-slugify) [![Package License](https://camo.githubusercontent.com/1a2e0606685ce00663bf829868f794fd3fc9c86f8d80cae324734129e0723a58/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE) [![Buy me a coffee](https://camo.githubusercontent.com/614d99c005f66fc0fe11f76717e097400f4a305862feacf21c994dd0efd61dcf/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76312e7376673f6c6162656c3d4275792532306d6525323061253230636f66666565266d6573736167653d25463025394625393225393326636f6c6f723d343334623537266c6f676f3d6275792532306d6525323061253230636f66666565266c6f676f436f6c6f723d7768697465266c6162656c436f6c6f723d313343334646)](https://www.buymeacoffee.com/sawastacks)

Contents
--------

[](#contents)

- [What is a slug?](#background-what-is-a-slug)
- [Installation](#installation)
- [Usage](#usage)

Simple Slugify Library for Codeigniter 4
========================================

[](#simple-slugify-library-for-codeigniter-4)

This library provides a way of generating a unique slugs.

 [ ![drawing](img/bmc.png) ](https://www.buymeacoffee.com/sawastacks)

Background: What is a slug?
---------------------------

[](#background-what-is-a-slug)

A slug is a simplified version of a string, typically URL-friendly. The act of "slugging" a string usually involves converting it to one case, and removing any non-URL-friendly characters (spaces, accented letters, ampersands, etc.). The resulting string can then be used as an identifier for a particular resource.

For example, if you have a blog with posts, you could refer to each post via the ID:

```
http://yourdomain.com/post/1
http://yourdomain.com/post/2

```

... but that's not particularly friendly (especially for [SEO](http://en.wikipedia.org/wiki/Search_engine_optimization)). You probably would prefer to use the post's title in the URL, but that becomes a problem if your post is titled "André &amp; François took 55% of the whole space in REAC Company", because this is pretty ugly too:

[http://yourdomain.com/post/Andr%C3%A9%20&amp;%20Fran%C3%A7ois%20took%2055%%20of%20the%20whole%20space%20in%20REAC%20Company](http://yourdomain.com/post/Andr%C3%A9%20&%20Fran%C3%A7ois%20took%2055%%20of%20the%20whole%20space%20in%20REAC%20Company)

The solution is to create a slug from the title and use that instead. You might want to use Codeigniter's built-in `\url_title('This is the title','-', true)` helper function to convert that title/string into slug as follow:

```
http://yourdomain.com/post/andre-francois-took-55-of-the-whole-space-in-reac-company

```

A URL like that will make users happier (it's readable, easier to type, etc.).

For more information, you might want to read [this](http://en.wikipedia.org/wiki/Slug_(web_publishing)#Slug) description on Wikipedia.

Slugs tend to be unique as well. So if you write another post with the same title, you'd want to distinguish between them somehow, typically with an incremental counter added to the end of the slug:

```
http://yourdomain.com/post/andre-francois-took-55-of-the-whole-space-in-reac-company
http://yourdomain.com/post/andre-francois-took-55-of-the-whole-space-in-reac-company-1
http://yourdomain.com/post/andre-francois-took-55-of-the-whole-space-in-reac-company-2

```

This keeps the URLs unique.

Installation
------------

[](#installation)

This package can be installed through `composer require`. Before install this, make sure that your are working with PHP &gt;= 7.2 in your system. Just run the following command in your cmd or terminal:

> Install the package via Composer:

```
composer require sawastacks/ci4-slugify
```

Usage
-----

[](#usage)

After package installed in your Codeigniter project, you have now many ways you will use to generate a unique slugs for your blog posts or products. Just follow the following guides below that will teach the way you will use SlugService Class inside your controller.

### Slugify Class

[](#slugify-class)

All the logic to generate slugs is handled by the `SawaStacks\CodeIgniter\Slugify` class.

Generally, you don't need to access this class directly, although there is one static method that can be used to generate a slug for a given string without actually creating or saving an associated model. All you need to do, is to use the below strategies in your controller in order to generate a unique slugs.

```
