0

我创建了一个依赖下拉列表,一切对我来说都很好。

CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('ajax/subcategories'),
'update'=>'#city_id', //selector to update
)));

AjaxController.php

class AjaxController extends Controller
{
...
}

现在我想隐藏 url “mydomain.com/ajax/subcategories”。如果有人尝试直接访问此 url,它将显示 404 错误页面。这可能吗?

4

1 回答 1

0

如果您正在发出 POST ajax 请求(看起来像您),您可以将您的操作包装在这样的检查中:

class AjaxController extends Controller {
  public function actionSubcategories() {
    if(Yii::app()->request->isPostRequest) { // check if POST
       // your action logic goes here
    } else { // direct URL request will be GET, so show error
      throw new CHttpException(404, Yii::t('app', 'Invalid request.'));
    }
  }
}

我对该 URL 的常规“直接”请求将是一个 GET 请求,因此这将向他们显示 404 错误。

于 2011-10-30T16:54:34.427 回答