0

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
// Symfony\Component\Routing\Exception\MethodNotAllowedException 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?

9
  • What are you trying to achieve exactly? I think I know how to do what you want, but I feel like you're trying to do something the wrong way. Are you trying to validate if a given URL exists in your app? Can you give some context please so I can try to provide the best solution for you? Commented Mar 12, 2023 at 18:13
  • 1
    "how can I inject Router (or whatever needed) into my validator" - through the DI container, as you inject all other services Commented Mar 13, 2023 at 6:50
  • @YannChabot "What are you trying to achieve exactly?". Made my example better. What I'm trying to achieve is dynamic routing, i.e user enters its name and I need to check if it does not collide with other users (easy) and with existing system urls (my question). Commented Mar 13, 2023 at 8:17
  • Please add all clarification to your question by editing it. Why not use a common URL for all users (like /user/$username) and check for existing usernames only? Commented Mar 13, 2023 at 11:00
  • 1
    @JohnSmith I think that you should compare against usernames instead of the URL if you just want to validate that it does not colide no? You could check via the UserRepository if the username is already taken instead of checking against the route? Or am I missing something? What is inside the route validator that you don't have anywhere else is my point. Commented Mar 13, 2023 at 13:20

2 Answers 2

0

How to make this into a Validator, or more specifically, how can I inject Router (or whatever needed) into my validator?

If you’re using default services configuration (autowire: true and autoconfigure: true), you can just inject needed dependencies into your validator in the same way as into other services.

Quick example below:

use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class MyValidator extends ConstraintValidator
{
    private RouterInterface $router;

    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    public function validate($value, Constraint $constraint): void
    {
         // ...
    }
}

See more in the Constraint Validators with Dependencies documentation paragraph.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I had hard time getting that to work - validator was not even being instantiated. But after some digging I found out that problem was that I was using CompoundConstraint and its validator is final class and therefore no DI for you, mr smarty pants.
0

I think you want one (or more) catch-all routes, for URLs which don't already have a controller

#[Route(path: '/{friendlyURL}', name: '...', methods: ['GET'], requirements: ['friendlyURL' => '[a-zA-Z0-9_-]{2,32}'], priority: -1, options: ['utf8' => true])]

The priority value means it gets checked last

1 Comment

Thanks, not quite what I asked but that would have been my next question :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.