【问题标题】:the controller function is not working in yii2控制器功能在 yii2 中不起作用
【发布时间】: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 做到了

标签: php yii yii2


【解决方案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',
        ],
    ];
}

【讨论】:

    猜你喜欢
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2019-08-23
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    相关资源
    最近更新 更多