BeSimpleSoap/Resources/doc/soapserver/tutorial/header.rst

90 lines
2.6 KiB
ReStructuredText
Raw Normal View History

2011-08-14 18:25:32 +02:00
Header
======
Controller
----------
.. code-block:: php
2013-02-20 15:58:12 +01:00
namespace Acme\DemoBundle\Controller;
2011-08-14 18:25:32 +02:00
use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;
use Symfony\Component\DependencyInjection\ContainerAware;
class DemoController extends ContainerAware
{
/**
* @Soap\Method("hello")
* @Soap\Header("api_key", phpType = "string")
* @Soap\Param("names", phpType = "string[]")
* @Soap\Result(phpType = "string")
*/
public function helloAction(array $names)
{
$soapHeaders = $this->container->get('request')->getSoapHeaders();
// You can use '1234' !== (string) $soapHeaders->get('api_key')
2011-08-14 18:25:32 +02:00
if (!$soapHeaders->has('api_key') || '1234' !== $soapHeaders->get('api_key')->getData()) {
throw new \SoapFault("INVALID_API_KEY", "The api_key is invalid.");
}
2013-02-20 15:58:12 +01:00
return "Hello ".implode(', ', $names);
2011-08-14 18:25:32 +02:00
}
}
Global header
-------------
If you want use a header for all actions of your controller you can declare the header like this:
.. code-block:: php
2013-02-20 15:58:12 +01:00
namespace Acme\DemoBundle\Controller;
use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @Soap\Header("api_key", phpType = "string")
*/
class DemoController extends ContainerAware
{
/**
* @Soap\Method("hello")
* @Soap\Param("names", phpType = "string[]")
* @Soap\Result(phpType = "string")
*/
public function helloAction(array $names)
{
2013-02-20 15:58:12 +01:00
return "Hello ".implode(', ', $names);
}
/**
* @Soap\Method("welcome")
* @Soap\Param("names", phpType = "string[]")
* @Soap\Result(phpType = "string")
*/
2011-09-24 12:44:10 +02:00
public function welcomeAction($names)
{
2013-02-20 15:58:12 +01:00
return "Welcome ".implode(', ', $names);
}
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer($container);
$this->checkApiKeyHeader();
}
private function checkApiKeyHeader()
{
$soapHeaders = $this->container->get('request')->getSoapHeaders();
// You can use '1234' !== (string) $soapHeaders->get('api_key')
if (!$soapHeaders->has('api_key') || '1234' !== $soapHeaders->get('api_key')->getData()) {
throw new \SoapFault("INVALID_API_KEY", "The api_key is invalid.");
}
}
2013-02-20 15:58:12 +01:00
}