0

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.

yivi
  • 42,438
  • 18
  • 116
  • 138
Arafor
  • 13
  • 2

2 Answers2

0

Depending on your symfony version, this can be done with specific typo:

https://symfony.com/blog/new-in-symfony-3-2-php-constants-in-yaml-files

Before 3.2 it is not possible from 3.2 to 3.4

arguments:
            - '@app.other_service'
            - !php/const:AppBundle\Entity\BlogPost::MUM_ITEMS
            - !php/const:Symfony\Component\HttpKernel\Kernel::VERSION

after 3.4:

arguments:
            - '@app.other_service'
            - !php/const AppBundle\Entity\BlogPost::MUM_ITEMS
            - !php/const Symfony\Component\HttpKernel\Kernel::VERSION

for your routing file:

# config/routes.yaml
blog_list:
    path:       /blog/{page}
    controller: AppBundle:SomeOtherDirectory/Something:index
    defaults:
        status: !php/const CompanyName\AppBundle\Entity\SomeDirectory\MyEntity::STATUS__CREATED
Florent Cardot
  • 1,400
  • 1
  • 10
  • 19
  • Tried your suggestion but it didn't work. I also tried to specify a constant that doesn't exist and didn't get an exception about it. So it seems that `!php/const` is still being ignored – Arafor Jun 18 '21 at 10:05
0

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.

yivi
  • 42,438
  • 18
  • 116
  • 138