43 lines
1.1KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\rest;
  8. use Yii;
  9. /**
  10. * OptionsAction responds to the OPTIONS request by sending back an `Allow` header.
  11. *
  12. * @author Qiang Xue <qiang.xue@gmail.com>
  13. * @since 2.0
  14. */
  15. class OptionsAction extends \yii\base\Action
  16. {
  17. /**
  18. * @var array the HTTP verbs that are supported by the collection URL
  19. */
  20. public $collectionOptions = ['GET', 'POST', 'HEAD', 'OPTIONS'];
  21. /**
  22. * @var array the HTTP verbs that are supported by the resource URL
  23. */
  24. public $resourceOptions = ['GET', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];
  25. /**
  26. * Responds to the OPTIONS request.
  27. * @param string $id
  28. */
  29. public function run($id = null)
  30. {
  31. if (Yii::$app->getRequest()->getMethod() !== 'OPTIONS') {
  32. Yii::$app->getResponse()->setStatusCode(405);
  33. }
  34. $options = $id === null ? $this->collectionOptions : $this->resourceOptions;
  35. Yii::$app->getResponse()->getHeaders()->set('Allow', implode(', ', $options));
  36. }
  37. }