在我正在创建的yii网站中,我有一个带有网址的页面,该网页以http://localhost/administrator/restaurant/list
表格格式显示餐馆列表以及删除按钮.删除按钮指向http://localhost/administrator/restaurant/delete/<id>.
在actionDelete我的控制器如下:
public function actionDelete(){
$model = Restaurants::model()->findByAttributes(
array(
'id'=>$_GET['id'],
'clientId'=>Yii::app()->user->clientId
));
$model->delete();
Yii::app()->user->setFlash('success',Yii::t('error','Restaurant has been deleted successfully'));
$this->redirect('restaurant/list',true);
}
Run Code Online (Sandbox Code Playgroud)
但是在单击"删除"按钮时,该行将从数据库中成功删除,但不是重定向到http://localhost/administrator/restaurant/list页面,而是重定向到http://localhost/administrator/restaurant/delete/restaurant/list并显示错误.我实现重定向功能的方式有问题吗?
改为使用数组路由:
$this->redirect(array('restaurant/list'), true);
Run Code Online (Sandbox Code Playgroud)
使用GET进行删除是一个非常糟糕的主意,因为浏览器可以在您点击之前预取链接.对于像这样的场景,你应该使用POST.