Tag

Symfony

18 posts tagged Symfony.

Posts

  • Diagram: a role hierarchy showing ROLE_ADMIN above ROLE_USER, beside three IS_AUTHENTICATED variants marked with which ones a plain visitor passes.

    09/02/2026

    is_granted in Twig: roles and IS_AUTHENTICATED

    is_granted takes more than roles, and picking the wrong one gives you a check that quietly passes when it should not. What each attribute tests, why role hierarchy makes ROLE_USER true for your admins, and where the check belongs.

  • Diagram: a crossed-out FOSUserBundle block opening into four replacement components -- Security, MakerBundle, ResetPasswordBundle and VerifyEmailBundle.

    09/02/2026

    Leaving FOSUserBundle: what replaces each piece

    The bundle is maintained only enough for existing projects to migrate off it. Every piece it provided now has a replacement in Symfony itself or in two small bundles - and the migration is mostly deletion.

  • Diagram: individually labeled form_row fields inside a form_start/form_end frame, with a form_rest chip carrying the CSRF token at the bottom.

    09/02/2026

    Rendering a Symfony form field by field in Twig

    One call renders the whole form and gives you no say in the markup. Splitting it up is four functions, and the one that matters most is form_rest - the safety net that stops a forgotten field from silently disappearing.

  • Diagram: autoescape on, punctured by |raw, with three contexts named where that filter is the wrong answer -- attribute, URL, inline JS and CSS.

    09/02/2026

    Twig autoescaping and the raw filter

    Autoescaping makes Twig safe by default and makes |raw the fastest way to undo that. The catch is that HTML escaping is only correct in HTML - inside a script tag or an unquoted attribute, the default is the wrong answer.

  • Diagram: a loop firing fifty queries at a database, compared against the same loop firing one query after addSelect().

    09/02/2026

    The N+1 you wrote in a Twig loop, and its fix

    The query looks fine and the template looks fine. Together they issue one query per row, because a lazy association is loaded the moment a template touches it - and templates touch things the controller never did.

  • Rendering Twig from Symfony Repositories Without DI

    07/04/2019

    Rendering a Twig template outside a controller

    You do not need a controller to render Twig, and you should not pass the container around to get at it. Inject the Environment, and use createTemplate when the template itself comes from the database.

  • Doctrine Arbitrary Table Joins on Unassociated Entities

    01/25/2016

    Doctrine: joining entities with no association

    In Symfony using Doctrine, if no association is available for two entities, you can still join them and treat them like they have a relationship. To do so, you have to use the “Join: WITH” method.

  • Diagram: a child template's block calling parent() to keep the base template's stylesheet lines and add its own, against a faint version where they were lost.

    11/30/2015

    Extending a parent Twig block with parent()

    Define a block in a child template and it replaces the parent's version completely - which is rarely what you wanted for a block holding stylesheets or scripts. parent() puts the inherited content back.

  • Diagram: addError() called from a controller landing on a specific field's inline error and on the form's own top-level error banner.

    06/11/2015

    Adding a form error from a controller in Symfony

    Some checks cannot live in a constraint because they need something the form does not know about. FormError attaches the message to a field, or to the form itself, so it renders where the user is looking.

  • Conditional User State in Twig Views

    04/28/2015

    Show the logged-in user in a Twig template

    The snippet everyone has saved uses app.user.username, which throws on every supported Symfony. Here is the version that works, which check to guard it with, and how to greet somebody by name rather than by email address.

  • Diagram: attribute(form, 'field_' ~ i) building a dynamic field name, resolving down to field_1 and field_2, guarded by an is defined check.

    04/14/2015

    Dynamic field names in Twig with attribute() and ~

    Dot notation cannot reach a property whose name you are building in the template. attribute() takes the name as a string, the tilde concatenates it, and `is defined` keeps the loop from throwing on a gap.

  • Diagram: a browser's cookie flowing into a Symfony Request object and out into a Twig template reading app.request.cookies.get('name').

    04/02/2015

    How to show or access cookie values in Symfony Twig views

    Strange how obscure it is to find a clear example of how to just access or show the value of a cookie in Twig, so here it is!

  • Doctrine ORM MySQL Field Type Definitions

    03/24/2015

    Doctrine field type mappings: tinyint is a boolean

    A MySQL tinyint is mapped to boolean regardless of its declared length, so a column holding 0 to 100 comes back as true. The full type table, and the two ways to get an integer back.

  • Diagram: the Request object at the center with arrows out to a Controller, a Service and a Twig template, and one arrow in from routing where the locale is set.

    01/01/2015

    Reading the current locale in Symfony

    The locale lives on the Request, not in a global. How to reach it from a controller, from a service that has no request, and from a template - and where it is set before any of that.

  • Diagram: setFirstResult and setMaxResults windowing a slice of a row stack, beside a fetch-joined collection case routed to Paginator instead.

    01/01/2015

    Limiting a Doctrine query with setMaxResults

    Here is an example of a simple DQL query in a custom repository method meant to return a single array result. The following will return the latest result in case there are multiple contact records that match the…

  • Diagram: MM and YYYY select fields feeding a DataTransformer that outputs a DateTime, beside a struck-through DateType with a hidden day control.

    11/19/2014

    A card expiry month/year field in Symfony

    The old answer was to render DateType's day dropdown and hide it with CSS. That works until somebody uses a screen reader. Two choice fields and a small transformer do the job honestly.

  • Diagram: a DataTransformer throwing an exception into a field that shows a generic message, beside an alternative field showing custom text built from the invalid value.

    08/20/2014

    invalid_message: what a failed transformer shows

    A transformer that cannot convert what was typed throws, and Symfony turns that into a message the user reads. Left alone it says nothing useful; invalid_message is where you replace it.

  • Diagram: Doctrine's flush() taking two paths, a silent commit or a thrown exception that has to be caught by type.

    11/19/2013

    Knowing whether a Doctrine insert or update worked

    Doctrine has no return value to check after flush() - it signals failure by throwing. Wrapping it is the right instinct, but the version of this snippet that has been circulating since 2013 catches nothing at all.