M
M
Mors Clamor2019-10-09 19:50:27
Yii
Mors Clamor, 2019-10-09 19:50:27

Yii2 RETSful API why Method Not Allowed?

The documentation for Yii2 quite clearly states -

GET /users: getting a page-by-page list of all users;
HEAD /users: get user listing metadata;
POST /users: create a new user;
GET /users/123: getting information on a specific user with id equal to 123;
HEAD /users/123: getting metadata for a specific user with id equal to 123;
PATCH /users/123 and PUT /users/123: change information for a user with id equal to 123;
DELETE /users/123: delete user with id equal to 123;
OPTIONS /users: get supported methods by which /users can be accessed;
OPTIONS /users/123: Get supported methods by which to access /users/123.

ok, created a controller
<?php

namespace app\controllers;

use yii\rest\ActiveController;

/**
 * PostController implements the CRUD actions for Posts model.
 */
class PostController extends ActiveController
{
  public $modelClass = 'app\models\Posts';
}
?>

As a result of a post request, I get a response to post, which says that only GET and HEAD are allowed. What is it like?
5d9e0fb0b0705378672537.png
Just in case, here is the urlManager listing
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing'=>true,
            'rules' => [
              'post'=>'post',
              'profile'=>'profile/index',
              'profile/verify/<hash:\w+>'=>'/auth/verify',
              'logout'=>'auth/logout',
              'register'=>'auth/register',
              ['class' => 'yii\rest\UrlRule', 'controller' => 'post'],
            ],
        ],

The request normally goes to post / create, but the essence of the RESTful API is generally in the use of HTTP verbs, and the documentation says the same.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mors Clamor, 2019-10-09
@66demon666

Adding a parameter to urlManager helped

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

D
Dmitry Derepko, 2019-10-09
@xEpozZ

1. Google your question link
2. Get the result link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question