Yii2 REST giving a 405 on POST, PUT, etc

user3627496 picture user3627496 · May 20, 2015 · Viewed 9.7k times · Source

I'm trying to build a REST API in a Yii2 advanced application for managing simple queries to my database. Following some tutorials, finally build step by step the example in the Guide, in Quick Start, and get my model working for GET and HEAD methods.

Created my API service inside a module, properly set, with minimal settings, request JSON parser on backend/main.php, registered the module in common/main.php and created the rule in urlManager there too (below the minimal usual rules, enablePrettyUrl, controller/action, ...):

['class' => 'yii\rest\UrlRule', 'controller' => ['precapi']]

But whenever I tried some other methods, via CURL or Postman REST Client, it always gives me a 405 error:

Method Not Allowed. This url can only handle the following request methods: GET, HEAD.

I think I have tried so many different configurations and paths trying to solve it, but without any result. Only thing is 'enableStrictParsing' => false' in urlManager, because it gives me a 404 error on some URLs, and POST does not work with this too, although the application is working as usual.

Any help will be appreciated. Regards.

Answer

Mijail Paz picture Mijail Paz · Dec 3, 2015

I think is pluralize issue, try this

['class' => 'yii\rest\UrlRule', 'controller' => 'precapi', 'pluralize'=>false],