Submitted by webmaster on May 16, 2016

If you have ever created a menu callback in Drupal 7 you are probably used to pulling your parameters from $_POST or some other PHP Global method.  If you are now looking at upgrading or building a module in Drupal 8, you may find yourself pulling your hair out trying to figure out why $_GET and $_POST are empty when you submit a page or do an ajax call to your newly created custom Drupal 8 REST endpoint.  

In Drupal 8 almost all remaining superglobals ($_GET, $_POST, etc) have been replaced by a Symfony Request Object. Using this object is not immediately apparent when your first creating your REST endpoints and can cause you a few hours of headaches unless you are aware of the change immediately.  In hopes of saving people from having that headache, I will provide some quick examples of what you need to do in order to be able to access $_GET or $_POST from your code again.

The first thing you need to do is take a look at your Routing.yml file for your endpoint and make sure that you are utilizing the method parameter in order to let Drupal 8 know what action you are taking.  I feel like this actually quite a brilliant way of handling these things especially when it comes to RESTful API's because you will be able to define different callback functions for the different accepted actions that you can take in Drupal 8, below is an example of how to do this.


Once you have setup your YAML file with the correct parameters you can then access your $_GET, $_POST, $_FILES, or other parameters using the syntax outlined below.  This also helps Drupal 8 be more secure than just directly accessing the old PHP globals like we did in Drupal 7 and earlier.


You can see a lot more information about using the new Request class provided by Symfony by reading through the documentation for the HttpFoundation component.