class apiConfiguration extends sfApplicationConfiguration
{
public function configure()
{
// ...
$this->dispatcher->connect('request.filter_parameters', array(
$this, 'requestFilterParameters'
));
}
public function requestFilterParameters(sfEvent $event, $parameters)
{
$request = $event->getSubject();
$api_key = $request->getHttpHeader('X_API_KEY');
if (null === $api_key || false === $api_user = Doctrine_Core::getTable('ApiUser')->findOneByToken($api_key))
{
throw new RuntimeException(sprintf('Invalid api key "%s"', $api_key));
}
$request->setParameter('api_user', $api_user);
return $parameters;
}
}
You will then be able to access your API user from the request:
public function executeFoobar(sfWebRequest $request)
{
$api_user = $request->getParameter('api_user');
}
This technique can be used, for example, to validate webservice calls.
Title:
Symfony - Write simple webservice with authentication header
Description:
class apiConfiguration extends sfApplicationConfiguration { public function configure() { // ... $this->dispatcher->connect(...
...
Rating:
4