PHPackages                             tomwilford/slim-sqids - 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. tomwilford/slim-sqids

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

tomwilford/slim-sqids
=====================

A package for using Sqids in Slim applications

v0.0.3(1y ago)03MITPHPPHP ^8.0

Since Feb 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/TomWilford/slim-sqids)[ Packagist](https://packagist.org/packages/tomwilford/slim-sqids)[ RSS](/packages/tomwilford-slim-sqids/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (8)Versions (4)Used By (0)

slim-sqids
==========

[](#slim-sqids)

This package helps you implement [Sqids](https://github.com/sqids/sqids-php) in [Slim](https://www.slimframework.com/). It provides a sqiddleware middleware that automatically decodes Sqids from URL parameters and a trait that adds a getter to return a Sqid-encoded value of a designated property.

Install
-------

[](#install)

Install via [Composer](https://getcomposer.org/):

```
composer require tomwilford/slim-sqids
```

Usage
-----

[](#usage)

Once slim-sqids is configured (see the Configuration section below), you can use it in your Slim application as follows:

#### Routing / Middleware

[](#routing--middleware)

1. **Register the Middleware:**

    Add the slim-sqids middleware before the routing middleware. This ensures that any route arguments containing "sqid" (case-insensitive) are decoded automatically.

```
$app = new Slim\App();

// Register slim-sqids middleware
$app->add(new \TomWilford\SlimSqids\SqidsMiddleware());
$app->addRoutingMiddleware();
```

2. **Define Routes with Sqid Parameters:**

    For any route that uses a Sqid, include the string "sqid" in the argument name. For example:

```
$app->get('/foos/{fooSqid}', \App\Action\Foo\Page\ShowAction::class);
```

In the above example, if a request is made to `/foos/UKkLWZg9DA`, the middleware decodes the parameter into `123`. Your controller then receives the decoded value:

```
public function __invoke(
    Request $request,
    Response $response,
    array $arguments = []
) {
    // 'fooSqid' now holds the decoded value (e.g., 123)
    $id = $arguments['fooSqid'];

    $foo = $this->repository->ofId($id);
    // ...
}
```

#### Sqid Getter

[](#sqid-getter)

To automatically add a `getSqid()` method to your class:

1. **Use the Trait:**

    Compose your class with the `HasSqidablePropertyTrait`:

```
class Foo
{
    use \TomWilford\SlimSqids\HasSqidablePropertyTrait;
    // ...
}
```

2. **Decorate the Property:**

    Use the PHP attribute `#[SqidableProperty]` to mark the property you want to encode. Note: if multiple properties are decorated, only the first one will be used by `getSqid()`.

```
class Foo
{
    use \TomWilford\SlimSqids\HasSqidablePropertyTrait;

    #[\TomWilford\SlimSqids\SqidableProperty]
    private int $id;
    // ...
}
```

Now, calling `getSqid()` on an instance of `Foo` returns the Sqid-encoded value of `$id`:

```
class Foo {
    // ...

    public function jsonSerialize(): mixed
    {
        return [
            'id'  => $this->getSqid(), // returns the encoded value (e.g., UKkLWZg9DA)
            'bar' => $this->bar,
        ];
    }
}
```

Configuration
-------------

[](#configuration)

There are two primary ways to configure slim-sqids, plus a hybrid approach if you wish to combine features.

#### Dependency Injection

[](#dependency-injection)

If your Slim application uses a dependency injection container:

1. **Register a Sqids Instance:**

    Add a new instance of Sqids to your container. See the [Sqids documentation](https://github.com/sqids/sqids-php?tab=readme-ov-file#-examples) for configuration examples.

```
// Example uses php-di/php-di
Sqids::class => function (ContainerInterface $container) {
    return new \Sqids\Sqids(
        minLength: 10
    );
},
```

2. **Inject into Your Classes:**

    Include Sqids in the constructor of any class that needs to encode a property:

```
class Foo
{
    use \TomWilford\SlimSqids\HasSqidablePropertyTrait;

    public function __construct(\Sqids\Sqids $sqids)
    {
        $this->sqids = $sqids;
    }
}
```

#### Global Configuration Class

[](#global-configuration-class)

If you are not using a dependency injection container, you can set a global configuration. A good location for this is your application's `bootstrap.php` file.

1. **Set the Global Configuration:**

```
\TomWilford\SlimSqids\GlobalSqidConfiguration::set(new \Sqids\Sqids(minLength: 10));
```

The global configuration is immutable - attempting to overwrite it will throw a `RuntimeException`. It will also throw an exception if accessed before being set.

#### Hybrid Approach

[](#hybrid-approach)

If you use a container but prefer not to inject Sqids directly into your classes (e.g., for models), you can combine the approaches:

1. **Register a Sqids Instance in Your Container:**

```
// Example uses php-di/php-di
Sqids::class => function (ContainerInterface $container) {
    return new \Sqids\Sqids(
        minLength: 10
    );
},
```

2. **Set the Global Configuration Using the Container:**

```
\TomWilford\SlimSqids\GlobalSqidConfiguration::set($container->get(\Sqids\Sqids::class));
```

Testing
-------

[](#testing)

The package includes tests using `PHPUnit`, `PHPStan`, and `PHP_CodeSniffer`. You can run all tests at once or individually via Composer:

```
composer test:all
```

```
# Run PHPUnit tests
composer test

# Run PHPStan (static analysis)
composer stan

# Run PHP_CodeSniffer (coding standards check)
composer sniffer:check
```

Contributing
------------

[](#contributing)

Contributions are welcome!

License
-------

[](#license)

The [MIT License (MIT)](LICENSE).

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance44

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

3

Last Release

439d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a82b2ac25a173ec4556a091b046ee9a269060f5a8d0a2b13eb3d4e490fee5fa5?d=identicon)[jollyBlueMan](/maintainers/jollyBlueMan)

---

Top Contributors

[![TomWilford](https://avatars.githubusercontent.com/u/28453594?v=4)](https://github.com/TomWilford "TomWilford (8 commits)")

---

Tags

middlewareslimsqids

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tomwilford-slim-sqids/health.svg)

```
[![Health](https://phpackages.com/badges/tomwilford-slim-sqids/health.svg)](https://phpackages.com/packages/tomwilford-slim-sqids)
```

###  Alternatives

[bryanjhv/slim-session

Session middleware and helper for Slim framework 4.

233961.5k16](/packages/bryanjhv-slim-session)[slim/csrf

Slim Framework 4 CSRF protection PSR-15 middleware

3512.1M94](/packages/slim-csrf)[cakephp/authentication

Authentication plugin for CakePHP

1153.6M67](/packages/cakephp-authentication)[slim/http-cache

Slim Framework HTTP cache middleware and service provider

1242.9M27](/packages/slim-http-cache)[jaxon-php/jaxon-core

Jaxon is an open source PHP library for easily creating Ajax web applications

73142.3k25](/packages/jaxon-php-jaxon-core)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)

PHPackages © 2026

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