OptionsAction.php 1.05 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\rest;

use Yii;

/**
 * OptionsAction responds to the OPTIONS request by sending back an `Allow` header.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class OptionsAction extends \yii\base\Action
{
	/**
	 * @var array the HTTP verbs that are supported by the collection URL
	 */
Qiang Xue committed
23
	public $collectionOptions = ['GET', 'POST', 'HEAD', 'OPTIONS'];
Qiang Xue committed
24 25 26
	/**
	 * @var array the HTTP verbs that are supported by the resource URL
	 */
Qiang Xue committed
27 28
	public $resourceOptions = ['GET', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];

Qiang Xue committed
29 30 31 32 33 34 35

	/**
	 * Responds to the OPTIONS request.
	 * @param string $id
	 */
	public function run($id = null)
	{
Qiang Xue committed
36 37 38
		if (Yii::$app->getRequest()->getMethod() !== 'OPTIONS') {
			Yii::$app->getResponse()->setStatusCode(405);
		}
Qiang Xue committed
39 40 41 42
		$options = $id === null ? $this->collectionOptions : $this->resourceOptions;
		Yii::$app->getResponse()->getHeaders()->set('Allow', implode(', ', $options));
	}
}