Download the PHP package laulamanapps/tokenable-symfony without Composer
On this page you can find all versions of the php package laulamanapps/tokenable-symfony. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laulamanapps/tokenable-symfony
More information about laulamanapps/tokenable-symfony
Files in laulamanapps/tokenable-symfony
Package tokenable-symfony
Short Description Reversible, type-prefixed, non-enumerable tokens for Doctrine entities in Symfony. Attribute-driven, with automatic route/URL encoding, controller argument resolution and a profiler panel.
License MIT
Informations about the package tokenable-symfony
laulamanapps/tokenable-symfony
Reversible, type-prefixed, non-enumerable tokens for your Doctrine entities — so you
never expose ?id=42 in a URL again. A token looks like per_3f9k2: a short type
prefix plus an obfuscated id.
Internally everything stays a typed integer id; at the HTTP boundary everything is an opaque, type-tagged, non-guessable token — with zero per-controller boilerplate.
The obfuscation uses Optimus (Knuth multiplicative hashing) so ids are not sequential or guessable, then base-36 encodes them for compactness.
Why
Sequential integer ids leak information (record counts, growth rate) and invite enumeration attacks. This bundle keeps ids private without you having to thread a separate "slug" column or hand-encode anything.
- Attribute-driven. Mark an entity
#[Tokenable(...)]— that's the whole opt-in. - Automatic in URLs. The router is decorated, so
path('route', {id: entity})(or an int) emits a token. Nothing to remember in templates. - Automatic in controllers. Type-hint the entity in your action and receive the loaded object — or a clean 404 for an invalid/unknown/mismatched token.
|tokenTwig filter for the odd manual case.- Profiler panel listing every token in/out of the request, plus config validation (duplicate prefixes, colliding triplets, out-of-range primes).
Requirements
- PHP 8.4+
- Symfony 7.1+ / 8.x
- Doctrine ORM 3+
Installation
Register the bundle (Symfony Flex does this automatically):
For the app:tokenable:generate helper command, also install phpseclib (dev only):
Configuration
Everything works out of the box; configuration is optional. Defaults shown:
separator lets you switch to e.g. - (giving per-3f9k2) if _ clashes with your routing
or slugs. It may be more than one character. base trades token length for the alphabet used
(base 16 → 0-9a-f, base 36 → 0-9a-z).
Changing
separatororbasechanges every token string, so treat them as set-once for a given deployment — existing URLs/bookmarks encoded under the old settings won't decode.
Usage
1. Mark an entity tokenable
Generate a fresh triplet:
Paste it onto the entity:
Each entity gets its own triplet so tokens never collide across types. The prefix must be
unique and must not contain the _ separator. getId() may return an int or any value
object exposing getValue(): int.
Inheritance (abstract base + discriminator map)
Declare #[Tokenable] once on an abstract Doctrine base that uses inheritance. Every
concrete subclass inherits the attribute and shares one prefix and one token space:
encode(new DirectDebitMandate(...)) produces an mnd_… token; decoding resolves the prefix
to the abstract base and Doctrine's discriminator loads the concrete subclass. Actions may
type-hint either the abstract base or a concrete subclass — a token pointing at a sibling
subclass then yields a 404. Do not add a second #[Tokenable] to the subclasses: a
duplicated prefix is rejected, and a distinct one would break the shared token space.
2. Generate tokens (automatic)
3. Resolve tokens (automatic)
The value resolver runs at priority 200 (before Doctrine's own EntityValueResolver). An
invalid token, an unknown prefix, or a prefix that resolves to the wrong entity type all
produce a 404 — unless the argument is nullable, in which case a not-found entity yields
null.
Programmatic access
Inject the Tokenizer service anywhere:
Debugging a token from the CLI
app:tokenable:convert converts a single identifier when you pass one, or drops into an
interactive REPL when you don't:
Commands
| Command | Purpose |
|---|---|
app:tokenable:generate <prefix> |
Generate a fresh prime/inverse/random triplet (needs phpseclib/phpseclib) |
app:tokenable:convert [token] |
Convert the given token/id directly, or run interactively when omitted |
How it fits together
| Service | Role |
|---|---|
Tokenizer |
Encode/decode; discovers #[Tokenable] entities via Doctrine metadata |
TokenableValueResolver |
Controller argument → loaded entity |
TokenableUrlGenerator |
Router decorator — encodes ids/entities in path()/generateUrl() |
TokenExtension |
The |token Twig filter (only registered when Twig is present) |
TokenCollector |
Web-profiler panel (only registered when the profiler is enabled) |
GenerateTokenableCommand / ConvertTokenCommand |
The console commands above |
Development
License
MIT — see LICENSE.
All versions of tokenable-symfony with dependencies
jenssegers/optimus Version ^1.1.2
doctrine/orm Version ^3.0
symfony/config Version ^7.1 || ^8.0
symfony/console Version ^7.3 || ^8.0
symfony/dependency-injection Version ^7.1 || ^8.0
symfony/http-foundation Version ^7.1 || ^8.0
symfony/http-kernel Version ^7.1 || ^8.0
symfony/routing Version ^7.1 || ^8.0