I need to check if entered value, e.g "specialist", is not in use as an URL in Symfony 6 application.
Simple ANY-method paths are simple:
/** @var RouterInterface $router */
$router->match('/specialist');
// returns array
Non-matching methods are also simple:
/** @var RouterInterface $router */
$router->match('/specialist');
// if route path "/specialist" accepts only POST
// SymfonyComponentRoutingExceptionMethodNotAllowedException is thrown
But I need to check urls that are used as a prefix, e.g if I need to check for string "login" and have urls ‘/login/facebook’ and ‘/login/google’ but no simple ‘/login’. So – how can I check if string is not being used as any url prefix?
Symfony version 6, as said.
EDIT:
How to make this into a Validator, or more specifically, how can I inject Router (or whatever needed) into my validator?
2
Answers
If you’re using default services configuration (
autowire: true
andautoconfigure: true
), you can just inject needed dependencies into your validator in the same way as into other services.Quick example below:
See more in the Constraint Validators with Dependencies documentation paragraph.
I think you want one (or more) catch-all routes, for URLs which don’t already have a controller
The priority value means it gets checked last