【发布时间】:2017-09-10 15:36:10
【问题描述】:
我是 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}
【问题讨论】:
-
请更新您的问题并添加相关的 url 调用并显示您的站点控制器代码
-
@scaisEdge 做到了