Home Blog Contact

How to get HTTP params from GET or POST requests in Shopware 6?

Jan 24th 2022

Do you know how to get GET or POST request arguments from the HTTP request? If not, let's check it out.

In case you don't know how to create a controller, please check this blogpost.

The following code will be our structure.

<?php declare(strict_types=1);

namespace MatheusGontijo\HelloWorld\Controller;

use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

 * @RouteScope(scopes={"storefront"})
class HelloWorldController extends StorefrontController
     * @Route("/hello-world", name="frontend.helloworld", methods={"GET","POST"})
    public function helloworld(Request $request)
        // code will be inserted here

Get all params

POST and GET methods work similarly. I'll give an example with GET, but POST request don't have the params passed on the url.

GET request: /hello-world?firstname=Matheus&lastname=Gontijo



    [firstname] => Matheus
    [lastname] => Gontijo

Get specific param

// result: Matheus

// result: Gontijo

Get specific param with default values, in case it was not passed

Let's say the param age is not passed, then you could set a default.

// if age is passed, then it will be the age from the parameter, let's say "30"
// result: 30

$request->query->get('age', 'None');
// but in case age is NOT passed, then it will be the age from the default parameter, let's say "None"
// result: None

Hope you enjoyed the blogpost ;- )

Extra links: