11

I ajaxly call a route and pass data to it, In my controller, I want to get $_POST['var'] , $_POST['var2'], it seems because of using HttpFoundation in drupal 8, $_POST and $_GET are not exists in old method, How I can access $_POST parameters in custom Controller?

  • 2
    $_POST still exists, you just shoudn't use it directly. If $_POST is empty, then the request object will also be empty and your error happens earlier. Often, this is for example because mod_rewrite is not enabled/configured correctly and the page is redirect through the not found directive, which drops post data. – Berdir Jul 14 '16 at 17:44
46

From the change record:

$name = $_POST['name']; // form param

becomes

$name = \Drupal::request()->request->get('name'); // form param

Incidentally, for GET vars, you would use:

$query = \Drupal::request()->query->get('name');
  • thanks, and do you know how can I get all items? I mean $_POST completely not one item. – Yusef Mohamadi Jul 14 '16 at 9:59
  • 2
    I haven't checked, but knowing Symfony I'd assume: \Drupal::request()->request->all() – Clive Jul 14 '16 at 10:02
  • 1
    Thanks, this is true answer to my question although I didn't get expected result ( maybe there is a problem in another place ,I ask it in another question. – Yusef Mohamadi Jul 14 '16 at 10:08
  • I can confirm that you can use \Drupal::request()->query->all() to get all $_GET variables. – oknate Jul 13 '17 at 14:26
19

In a controller get the request with a type-hinted argument Request $request:

<?php

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;

class ExampleController extends ControllerBase {

  /**
   * Controller to return a POST or a GET parameter.
   */
  public function action(Request $request) {

    // get your POST parameter
    $foo = $request->request->get('foo');

    // or get your GET parameter
    $foo = $request->query->get('foo');

    return ['#markup' => $foo];
  }

}

More info https://www.drupal.org/docs/8/api/routing-system/using-parameters-in-routes

9

Inject the RequestStack into your controller.

The current request contains the query attribute that in turn contains the GET parameters. request contains the POST parameters.

<?php

namespace Drupal\example_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * An example controller.
 */
class ExampleController extends ControllerBase {

  /**
   * @var Symfony\Component\HttpFoundation\RequestStack
   */
  private $requestStack;

  /**
   * Constructor.
   *
   * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
   */
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('request_stack')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function action() {
    // Get your GET parameter here.
    $this->requestStack->getCurrentRequest()->query->get('foo');
  }

}
3

You can check the Request object like

\Drupal::request()->getMethod();

it will returns GET or POST or whatever. If you are working inside an object, remember to DI the Request service.

0

None of the other answers worked for me but I found this that did:

$request->getContent();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.