Do you know how to extract the HTTP request's GET or POST request arguments? If not, let's have a look.

To get params we need a controller. If you have not created a controller yet, you can do this by How to create a controller in "Shopware 6"? article.

The following code describes how can we get the GET/POST parameters using the request's get method.

HelloWorldParamController.php

<?php declare(strict_types=1);

namespace MageSpark\HelloWorld\Controller;

use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @RouteScope (scopes={"storefront"})
 */
class HelloWorldParamController extends StorefrontController
{
    /**
     * @RouteScope (scopes={"storefront"})
     * @Route ("/get-http-param", name="frontend.get.httpParams", methods={"GET", "POST"})
     */
    public function getHttpData(Request $request, SalesChannelContext $salesChannelContext){

        
        $allParams = $request->query->all();


        echo "<pre><b>Result of \$allParams: </b><br/>";
        print_r($allParams);
        echo "</pre><br/>";


        /*----------------------------------------------------------------------------*/

        
        $firstName = $request->get("firstname");


        echo "<pre><b>Result of \$firstName: </b><br/>";
        print_r("First Name: ". $firstName);
        echo "</pre><br/>";


        /*----------------------------------------------------------------------------*/

        
        $defaultParamValue = $request->get("gender", "Not Specified");


        echo "<pre><b>Result of \$defaultParamValue: </b><br/>";
        print_r("Gender: ". $defaultParamValue);
        echo "</pre><br/>";

        exit;
    }
}

Result of the above code:

HTTP- parameter in shopware6

Obtain all parameters

The POST and GET methods function in the same way. I'll use GET as an example, although POST requests do not have the parameters supplied on the URI.

URL request to call your controller function

http://example.com/get-http-param?firstname=Darpan&lastname=Chaudhari

You can get all the parameters passed using GET or POST request as follow.

$request->query->all();

This function always returns an array containing all the parameters. If there is no parameter, the resulting array will be empty.

RESULT

Array (
    [firstname] => Darpan
    [lastname] => Chaudhari
)

Obtain specified parameters:

URL request to call your controller function

http://example.com/get-http-param?firstname=Darpan&lastname=Chaudhari

$request->get('firstname');

RESULT:  Darpan

$request->get('lastname');

RESULT:  Chaudhari

If a parameter was not given, get the default settings for it.

URL request to call your controller function

http://example.com/get-http-param?firstname=Darpan

$request->get('gender');

RESULT:  

In the given URL you can see that we have not passed gender parameter. So, the value will be null by default. because, we have not specified the default value. You can pass the default value as the second argument shown below.

$request->get('gender', 'Not Specified');

RESULT: Not Specified

 

We hope you enjoyed reading this post. Did you????

If you have any questions or discover any issues, please post them in the comments box below. We'd be delighted to assist you with this.

Remember to share this solution with your Shopware community or friends on social media, and stay tuned for future Shopware 6 tech tutorials.

Also, if you want to Develop Your Own Shopware Store do contact our certified shopware experts.

Have fun coding!!

Starting your own Shopware Store?

Get free consultation from Shopware experts on do's and don't