Hi,
Im trying to extend the Symfony Request class to clean up some code. As a example:
<?php
namespace Plugin\Name\Http\Requests;
use Symfony\Component\HttpFoundation\Request;
class ValidateExampleRequest extends Request
{
public function getEmail()
{
return $this->get('email');
}
}
After this I created a controller and injected my Request Class into the controller method.
<?php declare(strict_types=1);
namespace Plugin\Name\Http\Controllers;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
#[Route(defaults: ['_routeScope' => ['storefront']])]
class ExampleController extends StorefrontController
{
#[Route(path: '/example', name: 'storefront.plugin.name.example', methods: ['get'])]
public function example(ValidateExampleRequest $validateRequest)
{
return new JsonResponse([
'email' => $validateRequest->getEmail()
]);
}
}
After calling the URL I’m getting the following error:
Plugin\Name\Http\Controllers\ExampleController::example(): Argument #2 ($validateRequest) must be of type Plugin\Name\Http\Requests\ValidateExampleRequest, Symfony\Component\HttpFoundation\Request given, called in /var/www/html/vendor/symfony/http-kernel/HttpKernel.php on line 178
How can I inject my custom Request class to this controller method?