Как разрешить доступ к put и delete в restfull yii2?

Пользователь

от lamar , в категории: PHP , 8 месяцев назад

Как разрешить доступ к put и delete в restfull yii2?

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

1 ответ

Пользователь

от charles.orn , 7 месяцев назад

@lamar 

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',
                ]],
            ],
        ],
    ],
];


Replace 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.