Created
May 7, 2014 20:13
-
-
Save weierophinney/519924192ff1213546f4 to your computer and use it in GitHub Desktop.
Returning early if a specific HTTP header is missing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SomeApi; | |
use ZF\ApiProblem\ApiProblem; | |
use ZF\ApiProblem\ApiProblemResponse; | |
class Module | |
{ | |
public function onBootstrap($e) | |
{ | |
$app = $e->getTarget(); | |
$events = $app->getEventManager(); | |
$events->attach('route', array($this, 'onRoute'), -1000); | |
} | |
public function onRoute($e) | |
{ | |
$request = $e->getRequest(); | |
if (! method_exists($request, 'getHeaders')) { | |
// Console request? not sure what to do here, move along... | |
return; | |
} | |
if ($request->getHeaders()->has('Some-Specific-Header')) { | |
// Header is there, move along... | |
return; | |
} | |
return new ApiProblemResponse(new ApiProblem( | |
400, | |
'Missing Some-Specific-Header in the request!' | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment