Search code examples
phpsymfonysymfony-3.4symfony-routing

How to use PHP constant in routing YAML


I'm using Symfony 3.4

I managed to get the PHP constant to work for a service (as described in the documentation). But I can't figure out how to get it to work in a routing file. Here's what I have so far.

Entity:

namespace CompanyName\AppBundle\Entity\SomeDirectory;

class MyEntity
{
    public const STATUS__CREATED = 1;
}

routing.yml:

view_my_entity_with_status_created:
  path:       /created/
  defaults:
    _controller: "AppBundle:SomeOtherDirectory/Something:index"
    status: !php/const CompanyName\AppBundle\Entity\SomeDirectory\MyEntity::STATUS__CREATED

SomethingController:

public function indexAction(?int $status = null): Response
{
 // ...
}

From what I can tell the !php/const is being ignored since status is always null.


Solution

  • You can't in Symfony 3.4.

    Using !php/const in routing YAML configuration files was not enabled until Symfony 4.1.

    While the feature was added to YamlFileLoader in 3.2, it was only enabled by default for the dependency injection component, not for the routing component.

    This is when the feature was merged.