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 | |||
8 | namespace yii\rest; |
||
9 | |||
10 | use Yii; |
||
11 | |||
12 | /** |
||
13 | * OptionsAction responds to the OPTIONS request by sending back an `Allow` header. |
||
14 | * |
||
15 | * For more details and usage information on OptionsAction, see the [guide article on rest controllers](guide:rest-controllers). |
||
16 | * |
||
17 | * @author Qiang Xue <[email protected]> |
||
18 | * @since 2.0 |
||
19 | */ |
||
20 | class OptionsAction extends \yii\base\Action |
||
21 | { |
||
22 | /** |
||
23 | * @var array the HTTP verbs that are supported by the collection URL |
||
24 | */ |
||
25 | public $collectionOptions = ['GET', 'POST', 'HEAD', 'OPTIONS']; |
||
26 | /** |
||
27 | * @var array the HTTP verbs that are supported by the resource URL |
||
28 | */ |
||
29 | public $resourceOptions = ['GET', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Responds to the OPTIONS request. |
||
34 | * @param string $id |
||
35 | */ |
||
36 | public function run($id = null) |
||
37 | { |
||
38 | if (Yii::$app->getRequest()->getMethod() !== 'OPTIONS') { |
||
39 | Yii::$app->getResponse()->setStatusCode(405); |
||
40 | } |
||
41 | $options = $id === null ? $this->collectionOptions : $this->resourceOptions; |
||
42 | $headers = Yii::$app->getResponse()->getHeaders(); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
43 | $headers->set('Allow', implode(', ', $options)); |
||
44 | $headers->set('Access-Control-Allow-Methods', implode(', ', $options)); |
||
45 | } |
||
46 | } |
||
47 |