我是Yii的新手,我遇到了以下问题。下面是我编写的一个简单代码,用于搜索给定的ID并将关联的表行返回给用户。
use yii\rest\ActiveController;
use yii\web\Response;
use common\models\Restaurant;
use common\models\Address;
class DeliveryCostController extends ActiveController{
public $modelClass = 'common\models\Restaurant';
public function actionGetFixedCost($restaurantID = NULL){
$restaurant = Restaurant::findOne(['id'=>$restaurantID]);
if ($restaurant !== null) {
return [
'restaurant ID' => $restaurant['id'],
'restaurant Name' => $restaurant['name'],
'Fixed Delivery Cost' => $restaurant['fixed_delivery_cost']
];
}else{
return [
'Status' => 'Failed',
'Message' => 'Restaurant with ID ' . $restaurantID.' Not Found'
];
}
}
}
?>
我尝试访问此控制器:
http://localhost/food/api/web/deliverycost/getFixedCost?restaurantID=24
但我收到以下错误页面
An Error occurred while handling another error:
yii\base\InvalidRouteException: Unable to resolve the request "site/error". in C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Module.php:532
Stack trace:
#0 C:\xampp\htdocs\food\vendor\yiisoft\yii2\web\ErrorHandler.php(95): yii\base\Module->runAction('site/error')
#1 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#2 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#3 {main}
Previous exception:
yii\base\InvalidRouteException: Unable to resolve the request "deliverycost/getFixedCost". in
C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Module.php:532
Stack trace:
#0 C:\xampp\htdocs\food\vendor\yiisoft\yii2\web\Application.php(102): yii\base\Module->runAction('deliverycost/ge...', Array)
#1 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#2 C:\xampp\htdocs\food\api\web\index.php(17): yii\base\Application->run()
#3 {main}
Next yii\web\NotFoundHttpException: صفحهای یافت نشد. in C:\xampp\htdocs\food\vendor\yiisoft\yii2\web\Application.php:114
Stack trace:
#0 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 C:\xampp\htdocs\food\api\web\index.php(17): yii\base\Application->run()
#2 {main}
答案 0 :(得分:1)
基于Yii2 ruoting约定规则,你应该使用" get-fixed-cost"
http://localhost/food/api/web/deliverycost/get-fixed-cost?restaurantID=24
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
PS错误在你的信息中我与网站/控制器相关
检查站点/控制器中是否还有此代码
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}