Php 控制器功能在yii2中不工作
我是Yii的新手,我被以下问题困扰着。下面是我编写的一个简单代码,用于搜索给定ID并将关联的表行返回给用户。Php 控制器功能在yii2中不工作,php,yii,yii2,Php,Yii,Yii2,我是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
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}
根据Yii2 ruoting惯例规则,您应该使用“获取固定成本” PS与站点/控制器相关的消息i中的错误 检查站点/控制器中是否也有此代码
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
请更新您的问题并添加相关的url调用,同时显示您的站点控制器code@scaisEdge做到了
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}