diff --git a/Resources/doc/tutorial/header.rst b/Resources/doc/tutorial/header.rst index c3928df..687dfef 100644 --- a/Resources/doc/tutorial/header.rst +++ b/Resources/doc/tutorial/header.rst @@ -30,4 +30,60 @@ Controller return $this->container->get('besimple.soap.response')->setReturnValue("Hello ".implode(', ', $names)); } + } + +Global header +------------- + +If you want use a header for all actions of your controller you can declare the header like this: + +.. code-block:: php + + namespace My\App\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) + { + return $this->container->get('besimple.soap.response')->setReturnValue("Hello ".implode(', ', $names)); + } + + /** + * @Soap\Method("welcome") + * @Soap\Param("names", phpType = "string[]") + * @Soap\Result(phpType = "string") + */ + public function welcomeAction() + { + return $this->container->get('besimple.soap.response')->setReturnValue("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."); + } + } } \ No newline at end of file