How to access POST and GET parameters in Shopware 6
There are many situations, when you might need to access the values, sent by POST or GET method to your Shopware store, for example when you send them from a javascript plugin. And although you can always use the default PHP way, Shopware 6 has some methods of its own.
Access parameters in a controller
In a controller, you can easily access the POST and GET parameters by passing the corresponding argument to the method, that should handle the request. In the following example, there is a method from a controller. We take all parameters, that are passed to it and return them in JSON format:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/** * Method someAction * * @param Request $request * @param Context $context * @return JsonResponse * @Route("/api/v{version}/some-action", name="api.some.action", defaults={"auth_required"=false}, methods={"GET", "POST"}) */ public function someAction(Request $request, Context $context): JsonResponse { //prepare JSON response $response = new JsonResponse(); //get the parameters as an array $data = $request->request->all(); //put the data to the response $response->setData($data); //return the parameters in JSON format return $response; } } |
As you can see, the request is passed to your method in the form of an instance of class Symfony\Component\HttpFoundation\Request, along with the Context, that you will need for many other tasks inside your controller, for example to search in repositories etc.
The Request contains all the parameters from POST and GET methods in its property, aptly named request as well. You can retrieve them all with the all method or individually with the get method. You can also check for a specific parameter using the has method and set your own value to it, using the set method. All of them are in the example below:
1 2 3 4 5 6 7 8 |
//check if my parameter has a value $hasMyParameter = $request->request->has('my_parameter'); //get the value of my parameter $myParameter = $request->request->get('my_parameter'); //set a new value to my parameter $myParameter = $request->request->set('my_parameter', $newValue); |
You can limit the acceptable methods to just POST (data is sent from a form, for example) or GET (data is sent send as parameters from the URL), or allow both. In Shopware 6 (and Symfony framework, which it is based upon), this is done in the annotation just above the method declaration:
1 2 3 4 5 |
@Route( "/api/v{version}/some-action", name="api.some.action", defaults={"auth_required"=false}, methods={"GET", "POST"} ) |
The Request object does not have to contain just your parameters. You can also use it to get the files, that were uploaded through the form, for example. You can then access them like this:
1 2 |
//get the uploaded files $files = $request->files->all(); |
Access parameters in a subscriber (or elsewhere)
In Shopware 6, most of the time, you will use controllers to work with the data from a user-submitted form, but sometimes, you might need to access them from elsewhere in your code. This is more common for GET requests, that pass the parameters via the URL. Here is an example, how to get the parameter ‘test’ from inside a subscriber in the product detail page, when it was passed like this: www.some-store.xyz/some-product/?test=some-value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace TestPlugin\Subscriber; use Shopware\Storefront\Page\Product\ProductPageLoadedEvent; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class Subscriber implements EventSubscriberInterface { private $requestStack; public function __construct( RequestStack $requestStack ) { $this->requestStack = $requestStack; } public static function getSubscribedEvents(): array { return [ ProductPageLoadedEvent::class => 'onProductPageLoaded', ]; } public function onProductPageLoaded (ProductPageLoadedEvent $event) { //get current request from request stack $request = $this->requestStack->getCurrentRequest(); //get the value of the test parameter $test = $request->get('test'); } } |
In the end, it boils down to injecting the RequestStack and getting the current request from it. It contains all the parameters and you just need to use the get method to obtain their values. This works not just for subscribers, but in any class.
Is there a error?
did you mean $request->get instead of $request->request->get?
Greetings
No, there is no error. You can try it yourself. 🙂 The $request is an instance of Symfony\Component\HttpFoundation\Request and contains much more, than just an equivalent of PHP’s $_REQUEST.
You don’t need to inject RequestStack in a subscriber. The $event already has the request.
You can get the request with $request = $event->getRequest();