Why doesn't Extending the Action Argument Resolving in symfony4?

0 like 0 dislike
7 views
We will talk about this component:
https://symfony.com/doc/current/controller/argumen...

It seems to have done everything according to instructions, the code turned out like this:
namespace App\\ArgumentResolver; use App\\Domain\\VO\\UserId; use Generator; use Symfony\\Component\\HttpFoundation\equest; use Symfony\\Component\\HttpKernel\\Controller\\ArgumentValueResolverInterface; use Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadata; class UserIdResolver implements ArgumentValueResolverInterface { public function supports(Request $request, ArgumentMetadata $argument): bool { return UserId::class === $argument->getType(); } public function resolve(Request $request, ArgumentMetadata $argument): Generator { yield new UserId($request->get('userId')); } }

namespace App\\Domain\\VO; class UserId { private $value; public function __construct(string $value) { $this->value = $value; } public function __toString(): string { return $this->value; } }

namespace App\\Controller; use App\\Domain\\VO\\UserId; use FOS\estBundle\\Controller\\Annotations as Rest; use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\ParamConverter; class UserController { /** * @Rest\\Get("/users/{userId}") */ public function getUserById(UserId $userId): void { var_dump($userId); die('1234'); } }

services: # default configuration for services in *this* file _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. public: false # Allows optimizing the container by removing unused services; this also means # fetching services directly from the container via $container->get() won't work. # The best practice is to be explicit about your dependencies anyway. # makes the classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name App\\: resource: '../src/*' exclude: '../src/{Domain,Entity,Migrations,Tests,Kernel.php}' # controllers are imported separately to make sure services can be injected # as action arguments even if you don't extend any controller base class App\\Controller\\: resource: '../src/Controller' tags: ['controller.service_arguments'] App\\ArgumentResolver\\UserIdResolver: class: App\\ArgumentResolver\\UserIdResolver tags: - { name: controller.argument_value_resolver, priority: 50 }

"require": { "php": "^7.1.3", "ext-iconv": "*", "friendsofsymfony/rest-bundle": "^2.3", "jms/serializer-bundle": "^2.3", "sensio/framework-extra-bundle": "^5.1", "symfony/apache-pack": "^1.0", "symfony/console": "^4.0", "symfony/flex": "^1.0", "symfony/framework-bundle": "^4.0", "symfony/lts": "^4@dev", "symfony/yaml": "^4.0" }, "require-dev": { "symfony/dotenv": "^4.0", "symfony/web-server-bundle": "^4.0" },


The whole folder of the Domain I added to exclude, otherwise there is a conflict with autowiring (trying to create an instance of the UserId, but stumbles upon an unknown argument to the constructor).

At the moment it gives an error that the first argument is an int, not a UserId. Code in class UserIdResolver does not start (does not respond to die())? although the class is loaded. Changing priority does not help.

CANT?
by | 7 views

1 Answer

0 like 0 dislike
The problem is that RequestAttributeValueResolver fulfills early, consequently your resolver is not reached.

Solution:
1. To increase the priority (more than 100)
App\\ArgumentResolver\\UserIdResolver: class: App\\ArgumentResolver\\UserIdResolver tags: - { name: controller.argument_value_resolver, priority: 101 }

2. In the settings of the router to use a different variable name that does not coincide with the parameters of the action
class UserController { /** * @Rest\\Get("/users/{userId}") */ public function getUserById(UserId $id): void { var_dump($id); die('1234'); } }

Then we can get rid of config
App\\ArgumentResolver\\UserIdResolver: class: App\\ArgumentResolver\\UserIdResolver tags: - { name: controller.argument_value_resolver, priority: 50 }

since the tag controller.argument_value_resolver is added automatically based on the interface
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
33,932 users