Skip to content

Instantly share code, notes, and snippets.

@baisong
Created June 9, 2016 01:03
Show Gist options
  • Save baisong/5fb45a600fd9d31e5c9e2e501073e2e5 to your computer and use it in GitHub Desktop.
Save baisong/5fb45a600fd9d31e5c9e2e501073e2e5 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\mymodule\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Class FooBarController.
*
* @package Drupal\mymodule\Controller
*/
class FooBarController extends ControllerBase {
/**
* Provides content for the /mymodule/foobar path.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request
*/
public function foobar(Request $request) {
$content = $request->getContent();
$data = array();
$data['a'] = $request->get('foo');
$data['b'] = $request->request->get('foo');
$data['c'] = \Drupal::requestStack()->getCurrentRequest()->request->get('foo');
$data['d'] = $request->request->all();
$data['e'] = $content;
$request = \Drupal::requestStack()->getCurrentRequest();
$content = $request->getContent();
$data['f'] = $request->get('foo');
$data['g'] = $request->request->get('foo');
$data['h'] = $request->request->all();
$data['i'] = $content;
return new JsonResponse((object) $data);
}
// Return value:
// {
// "a": null,
// "b": null,
// "c": null,
// "d": [],
// "e": "",
// "f": null,
// "g": null,
// "h": [],
// "i": ""
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment