By default, Yii2's RESTful API only allows GET, POST and HEAD HTTP methods. If you want to allow PUT and DELETE methods for your RESTful API, you can configure it in your Yii2 application's configuration file (usually
config/web.php) by adding the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
return [ // ... 'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ // ... ['class' => 'yii estUrlRule', 'controller' => 'your-controller', 'extraPatterns' => [ 'PUT <id>' => 'update', 'DELETE <id>' => 'delete', ]], ], ], ], ];
your-controller with the name of your RESTful API controller. This configuration will enable pretty URLs, strict parsing of URLs, and allow PUT and DELETE methods for updating and deleting resources.
Note that enabling PUT and DELETE methods in your RESTful API should be done with caution, as these methods can modify or delete resources permanently. It is important to implement proper authentication and authorization mechanisms to ensure that only authorized users can access these methods.