Yii2:找不到我的新动作 - 404

时间:2017-01-24 13:03:24

标签: yii2 yii2-advanced-app yii-actions

我正在尝试通过ajax调用自定义操作,但返回的响应是404,我很确定它是路由问题,但我无法弄清楚如何解决它。 这是我的代码:

动作

public function actionGetOne($id){
    $model = Driver::findOne($id);
    if(!empty($model)){
        $data = [];
        $row = [
            'id'=>$model->id,
            'full_name'=>$model->full_name,
            'email'=>$model->email,
            'nationality_id'=>$model->nationality_id,
            'current_location'=>$model->current_location,
            'medical_check_id'=>$model->medical_check_id,
            'img'=>$model->img,
            'current_fleet_id'=>$model->current_fleet_id,
            'availability'=>$model->availability
        ];
        $data[] = $row;
        echo json_encode(['driver-getOne'=>'success','data'=>$data]);
    } else{
        echo json_encode(['driver-getOne'=>'failure']);
    }
}

AJAX

$.ajax({
    url:'<?= urldecode(Url::toRoute(['driver/get-one'])); ?>?id=<?= $id; ?>',
    method:'post',
    dataType:'json',
    success:function(response){}
    error:function(){
        alert('target action is not found!');
    }
}

后端/配置/ params.php

<?php
return [
    'adminEmail' => 'admin@example.com',
    'urlRules' => [
        '' => 'site/index',
        'login/' => 'site/login',
        'signup/' => 'site/signup',
        '<controller:[\w-]+>/<action:\w+>' => '<controller>/<action>',
        '<controller:[\w-]+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:[\w-]+>/<id:\d+>' => '<controller>/view',
        '<controller:[\w-]+>/create' => '<controller>/create',
        '<controller:[\w-]+>/update/<id:\d+>' => '<controller>/update',
        '<controller:[\w-]+>/delete/<id:\d+>' => '<controller>/delete',
        '<controller:[\w-]+>/get-all' => '<controller>/get-all',
        '<controller:[\w-]+>/get-one' => '<controller>/get-one',
        '<controller:[\w-]+>/update-status' => '<controller>/update-status',
    ]
];

1 个答案:

答案 0 :(得分:1)

改变一些事情,然后再试一次。

动作:

public function actionGetOne($id)
{
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    $model = Driver::findOne($id);
    if (empty($model)) {
        return ['driver-getOne' => 'failure'];
    }
    return [
        'driver-getOne' => 'success',
        'data' => [[
            'id' => $model->id,
            'full_name' => $model->full_name,
            'email' => $model->email,
            'nationality_id' => $model->nationality_id,
            'current_location' => $model->current_location,
            'medical_check_id' => $model->medical_check_id,
            'img' => $model->img,
            'current_fleet_id' => $model->current_fleet_id,
            'availability' => $model->availability
        ]],
    ];
}

行动应return正确完成响应顺序,否则可能会发生不必要的事情。通过设置响应格式,您可以自动获得JSON编码数组。

AJAX:

$.ajax({
    url:'<?= Url::to(['driver/get-one', 'id' => $id]) ?>',
    method:'post',
    dataType:'json',
    success:function(response){}
    error:function(){
        alert('target action is not found!');
    }
}

使用正确的语法获取您的网址。

PARAMS:

'urlRules' => [
    '' => 'site/index',
    'login' => 'site/login',
    'signup' => 'site/signup',
    '<controller:[\w-]+>/<id:\d+>' => '<controller>/view',
    '<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => '<controller>/<action>',
    '<controller:[\w-]+>/<action:[\w-]+>' => '<controller>/<action>',
]

我假设您将urlRules传递给components > urlManager > rules,否则网址规则将无效 我删除了多余的规则。一般来说,最后添加一般规则,首先添加特定规则。