This is api url.
/api/web/v1/users/123
Find user by id. How to change rule to find by token
, not by id
?
Here is a rule:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/users'],
'tokens' => [
'{id}' => '<id:\\w+>'
// this does not work
// '{token}' => '<token:\\w+>'
],
],
That
{id}
defined as a token is the one used by the built in yii\rest\ViewAction which code is this:$this->findModel($id)
is defined under yii\rest\Action and it find models using their primary key(s). If you need to use a different token like{token}
and find your model within a different attribute than its primary key then you'll need to override the view action inside your controller.Here is an example that should work when adding
'{token}' => '<token:\\w+>'
to your rules:Also note that you will need to change your $patterns to support the new inroduced one. Your final rules may look like this: