0

I have created a blank Yii 2 project that have created a REST UserController for already existing User model:

namespace app\controllers;
use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';
}

I have modified the model to have all fields safe:

public function rules()
{
    return [
        ['status', 'default', 'value' => self::STATUS_INACTIVE],
        ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
        [['username', 'email'], 'required'],
        [['username', 'email'], 'unique'],
        ['email', 'email'],
        [['password_hash', 'password_reset_token', 'verification_token', 'auth_key', 'status,created_at', 'updated_at', 'password'], 'safe'],
    ];
}

I have configured URL rules to have both pluralized and non-pluralized paths:

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => 'user',
            'pluralize' => false,
            'except' => ['index'],
        ],
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => 'user',
            'patterns' => [
                'GET,HEAD,OPTIONS' => 'index',
            ],
        ],
    ],

I have enabled JSON input, if that matters:

'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

All the verbs are processed correctly except for OPTIONS /users:

When I execute OPTIONS /user/20 then I am getting:

  • 200 OK
  • Empty content
  • List of allowed methods

But, when I execute OPTIONS users then I am getting 405 Method not Allowed.

enter image description here

What can be wrong or what am I missing?

trejder
  • 17,148
  • 27
  • 124
  • 216
  • 1
    Does it work when you change url rule from `'patterns' => ['GET,HEAD,OPTIONS' => 'index'],` to `'patterns' => ['GET,HEAD' => 'index', '' => 'options'],`? – Bizley Jun 10 '22 at 08:25
  • @Bizley Yes, this is one of the solutions that worked. The other one is given below. Thank you. – trejder Jun 10 '22 at 21:45

1 Answers1

1

You are getting 405 Method Not Allowed not because of routing but because of yii\filters\VerbFilter.

The yii\rest\Controller uses verbs() method to set up VerbFilter. The yii\rest\ActiveController overrides verbs() method and sets VerbFilter to only allow GET and HEAD requests for index action. It uses options action for OPTIONS method.

If you really want to use index action for OPTIONS method. You have to override verbs() method yourself and add OPTIONS as allowed method for that action. For example like this:

protected function verbs()
{
    $verbs = parent::verbs();
    $verbs['index'][] = 'OPTIONS';
    return $verbs;
}

Or if you want to use options action you have to modify patterns settings as suggested by @Bizley in comments.

Michal Hynčica
  • 5,038
  • 1
  • 12
  • 24