Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using argument resolving instead of paramconverter #9

Open
garak opened this issue Apr 5, 2019 · 1 comment
Open

Using argument resolving instead of paramconverter #9

garak opened this issue Apr 5, 2019 · 1 comment

Comments

@garak
Copy link

garak commented Apr 5, 2019

I think that using argument resolving instead of paramconverter could be a more elegant solution.

Working example from a project of mine:

<?php

namespace App\ArgumentResolver;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;

final class UuidResolver implements ArgumentValueResolverInterface
{
    public function supports(Request $request, ArgumentMetadata $argument): bool
    {
        return UuidInterface::class === $argument->getType();
    }

    public function resolve(Request $request, ArgumentMetadata $argument): ?\Generator
    {
        yield Uuid::fromString($request->attributes->get('id'));
    }
}

Then you don't need any annotation, type-hinting it's enough.
Action example:

/**
 * @Route("/simple/{uuid}")
 */
public function simpleAction(UuidInterface $uuid)
{
    return new Response($uuid->toString());
}
@mcfedr
Copy link
Member

mcfedr commented Apr 5, 2019

Looking good, havent come across that feature before - looks basically the same, though good that doesnt require an extra bundle - the param converter also works without any annotations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants