Php Yii2根据控制器rest api修改404消息
我正在开发Php Yii2根据控制器rest api修改404消息,php,rest,yii2,Php,Rest,Yii2,我正在开发Yii2restapi,所有API都工作正常。我只想知道如何根据控制器及其操作修改404错误消息 比如说 我调用api/users/100,当没有id为100的用户时,响应为 { "name": "Not Found", "message": "Object not found: 55554", "code": 0, "status": 404, "type": "yiiwebNotFoundHttpException" } 我在web.php 'urlManager' =>
Yii2
restapi,所有API都工作正常。我只想知道如何根据控制器及其操作修改404错误消息
比如说
我调用api/users/100
,当没有id为100的用户时,响应为
{
"name": "Not Found",
"message": "Object not found: 55554",
"code": 0,
"status": 404,
"type": "yiiwebNotFoundHttpException"
}
我在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 is denied due to 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,
// ...
],
],
],
“urlManager”=>[
“enablePrettyUrl”=>true,
'enableStrictParsing'=>false,
'showScriptName'=>false,
“规则”=>[
''=>'站点/',
“users/”=>“users/”,
]
]
“响应”=>[
'class'=>'yii\web\Response',
'on beforeSend'=>函数($event){
$response=$event->sender;
如果($response->data!==null&&Yii::$app->request->get('suppress\u response\u code')){
$response->data=[
“成功”=>$response->issusccessful,
“数据”=>$response->data,
];
$response->statusCode=200;
}else if($response->statusCode==404){
$response->data=[
“成功”=>错误,
“未找到消息'=>'资源”。”,
];
$response->statusText=json_encode(['success'=>false,'message'=>'未找到资源]);
}else if($response->statusCode==401){
$response->statusText=json_encode(['success'=>false,'message'=>'Unauthorized:由于密钥无效,访问被拒绝]);
}
},
“格式化程序”=>[
\yii\web\Response::FORMAT_JSON=>[
'class'=>'yii\web\jsonresponseformter',
'prettyPrint'=>YII_DEBUG,//在调试模式下使用“prettyPrint”输出
“encodeOptions”=>JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
// ...
],
],
],
但现在这是所有404错误的常见消息
我想要的是,如果它是为用户,那么消息应该是
找不到用户
如果是类别
找不到类别
所有这些API都遵循默认的Yi2 REST API标准。如果您使用的是
yii\REST\ActiveController
,您只需使用自定义的findModel
方法(无需像以前那样修改响应)
在控制器中:
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");
}
}
编辑:
users/sdafsda
不是由您的用户控制器处理的,您应该修复您的url规则:users/
而不是users/
如果您使用的是yii\rest\ActiveController
,您可以简单地使用自定义的findModel
方法(无需像您那样修改响应)
在控制器中:
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");
}
}
编辑:
users/sdafsda
不是由您的用户控制器处理的,您应该修复您的url规则:users/
而不是users/
当我们传递字符串时,甚至findModel
也没有调用<代码>用户/sdafsda。再问一个问题。如果我有两个URL“users/”=>“users/user by email”和“users/”=>“users/”,其中第一个用于通过email进行搜索。当我们传递字符串时,甚至findModel
都没有调用<代码>用户/sdafsda。再问一个问题。如果我有两个URL“users/”=>“users/user by email”和“users/”=>“users/”,其中第一个用于通过email进行搜索。