Colgate Shu Colgate Shu - 1 year ago 128
PHP Question

In symfony, how can i get Global methods and variables in any Controller?

The scene is, the client (All request like Ajax in my site) will request a json string like that

.PHP receive it by
it. I store it in a variable named
so i can get it in any Controller.

I also create a method to get value by key, code like that

public function input($key, $default = '', $func = '')
$ret = '';
if (isset($this->data[$key])) {
$ret = $this->data[$key];
} else {
return $default;
if (is_string($func)) {
if (in_array($func, ['int', 'string', 'array'])) {
settype($ret, $func);
return $ret;
if ($func)
$ret = call_user_func($func, $ret);
} elseif (is_array($func)) {
if ($func)
$ret = call_user_func_array($func, [$ret]);
if (!$ret)
return $default;
return $ret;

Although i can reg it to server, but i use it like that
, this will appear in any controller. Is this a proposed solution?
The question just my title, HOW? Save me .

Answer Source

If a method is in controller, then you can add $request object to params:

use Symfony\Component\HttpFoundation\Request;
public function input(Request $request, $key, $default = '', $func = '')

Request object has all global vars. More info:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download