php - Yii2 Modify 404 message according to the controller rest api -


i working on yii2 rest api , apis working fine. want know how can modify 404 error message according controller , actions.

for example.

i call api /users/100 , response, when there no user 100 id, is

{ "name": "not found", "message": "object not found: 55554", "code": 0, "status": 404, "type": "yiiwebnotfoundhttpexception" }  

i have modified response following code in web.php

 'urlmanager' => [             'enableprettyurl' => true,             'enablestrictparsing' => false,             'showscriptname' => false,             'rules' => [                           '<alias:index|about|contact|login|doc>' => 'site/<alias>',   'users/<id:\d+>' => 'users/', ] ]  'response' => [             'class' => 'yii\web\response',             'on beforesend' => function ($event) {                 $response = $event->sender;                  if ($response->data !== null && yii::$app->request->get('suppress_response_code')) {                     $response->data = [                         'success' => $response->issuccessful,                         'data' => $response->data,                     ];                     $response->statuscode = 200;                 } else if ($response->statuscode == 404) {                     $response->data = [                         'success' => false,                         'message' => 'resource not found.',                     ];                     $response->statustext = json_encode(['success' => false, 'message' => 'resource not found.']);                 } else if ($response->statuscode == 401) {                     $response->statustext = json_encode(['success' => false, 'message' => 'unauthorized: access denied due invalid key.']);                 }             },                     'formatters' => [                         \yii\web\response::format_json => [                             'class' => 'yii\web\jsonresponseformatter',                             'prettyprint' => yii_debug, // use "pretty" output in debug mode                             'encodeoptions' => json_unescaped_slashes | json_unescaped_unicode,                         // ...                         ],                     ],                 ], 

but common message 404 errors.

what want is, if user message should be

no user found

if categories

no categories found

all these apis following default yii2 rest api standards.

if using yii\rest\activecontroller, use custom findmodel method (no need modify response did).

in controller :

public function actions() {     $actions = parent::actions();      // customize findmodel     $actions['view']['findmodel'] = [$this, 'findmodel'];      return $actions; }  public function findmodel($id) {     $model = user::findone($id);      if (isset($model )) {         return $model ;     } else {         throw new notfoundhttpexception("no user found");     } } 

edit :

users/sdafsda not handled user controller, should fix url rule : users/<id> instead of users/<id:\d+>.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -