index uses the super-global variable $_REQUEST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies
of your class. This makes your code less dependent on global state and it
becomes generally more testable:
// BadclassRouter{publicfunctiongenerate($path){return$_SERVER['HOST'].$path;}}// BetterclassRouter{private$host;publicfunction__construct($host){$this->host=$host;}publicfunctiongenerate($path){return$this->host.$path;}}classController{publicfunctionmyAction(Request$request){// Instead of$page=isset($_GET['page'])?intval($_GET['page']):1;// Better (assuming you use the Symfony2 request)$page=$request->query->get('page',1);}}
Loading history...
13
{
14
try {
15
if (!array_key_exists('event', $_REQUEST)) {
16
throw new Exception('No event defined');
17
}
18
19
if (!array_key_exists('context', $_REQUEST)) {
20
throw new Exception('No context defined');
21
}
22
23
$context = json_decode($_REQUEST['context']);
24
25
if (!$context) {
26
throw new Exception('Invalid context');
27
}
28
29
$subscription = $event->getSubscription();
30
31
if (!array_key_exists($_REQUEST['event'], $subscription)) {
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: