deviloper
deviloper

Reputation: 7240

the controller function is not working in yii2

I am new to Yii and am stuck with the following issue. Below is a simple code I have written to search a given ID and return the associated table row to the user.

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'
        ];
    }
}
}
?>

I try to access this controller as: http://localhost/food/api/web/deliverycost/getFixedCost?restaurantID=24But I get the following error page

    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}

Upvotes: 1

Views: 1662

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

based on Yii2 ruoting convention rules you should use "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 the error in you msg i related to site/controller

check if you have also this code in site/controller

/**
 * @inheritdoc
 */
public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
    ];
}

Upvotes: 1

Related Questions