Description
Symfony version(s) affected
6.3.3
Description
Within 6.3 and higher, Symfony can now use Backed Enums to generate route paths like so:
Lets say I have case FOO_TYPE = 'bar';
as Enum avaiable. And this route:
#[Route(
path: 'some/interesting/path/{someValue}/{someOtherValue}',
name: 'some-interesting-path-with-two-variables',
methods: ['POST']
)]
public function someInterestingPathWithTwoVariables(string $someValue, MyEnum $someOtherValue = MyEnum:FIRST_OPTION): Response {}
This works. It makes the path available correctly: /some/interesting/path/foo/bar
.
Now, if I use the UrlGenerationInterface
classes, and attempt to generate the URL accordingly:
$this->generator->generate('some-interesting-path-with-two-variables', [
'someValue' => 'foo',
'someOtherValue' => $myEnumCase->name
])
I expect this URL: /some/interesting/path/foo/bar
But I get /some/interesting/path/foo/FOO_TYPE
, which is not resolvable and leads to a 404.
What also works is to specifiy the value
of the enum, but it results in this URL: ``/some/interesting/path/foo`.
What does not work at all (but should!) is passing the Enum Case directly. In this case however, the error is:
Object of class App\Components\Types\MyEnum could not be converted to string
Since you cannot integrate a custom __toString into an enum, thats not an option to work around it as well.
How to reproduce
Code examples are in the description, as the description would not make much sense without examples.
Please note that these are direct copies and I renamed values and parameters only to show a PoC.
Possible Solution
Generator (Symfony\Bundle\FrameworkBundle\Rounting\Router
) should handle enums correctly and use the scalar value of the enum to generate URLs.
Additional Context
No response