- Symfony's convenience methods for automatically fetching database entities from URL parameters are super-handy.
- Obfuscated/hash IDs are a great idea, especially in APIs (where you aren't concerned with SEO, but might be concerned about sequential numeric ids or exposing database information).
- Here's how to make them work together.
The stuff in this gist sets up a Twig filter (obfuscate
) to create the obfuscated ids (for URLs), makes the obfuscator available as a service (id_obfuscator
) so you can also generate obfuscated URLs in your controllers or whatever, and extends the DoctrineParamConverter to allow it to retrieve entities by their deobfuscated id.
Following Phil Sturgeon's excellent advice in Build APIs You Won't Hate, I've also added an option to allow multiple ids to be loaded at once, like /resources/id1,id2,id3,id4
. It's really quite handy sometimes. Bewarned though; it won't