yii post delete request more safe

 

 

常规的delete方法如下:

/** 
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*/
public function actionDelete()

if(Yii::app()->request->isPostRequest)

// we only allow deletion via POST request
$this->loadModel()->delete();

// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(array('index'));

else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}

 


 

转载请注明:PHP攻城师 

http://blog.csdn.net/phpgcs/article/details/10393305

    


通过POST请求来删除,会弹出对话框,让用户确认,更安全些。

在CGridView 中 ,会自动的发送POST 请求。

122 array(
123 'headerHtmlOptions'=>array('width'=>'60px'),
124 'class'=>'CButtonColumn', 'header'=>'操作',
125 'template'=>'{view} {update} {delete}',
126 'buttons'=>array(
127 'view'=>array(
128 'label'=>'查看',
129 'url'=>'Yii::app()->createURL("supervise/default/view", array("id"=>$data->id))',
130 'imageUrl'=>Yii::app()->baseUrl.'/images/icons/user.png',
131 ),
132 'update'=>array(
133 'label'=>'修改',
134 'url'=>'Yii::app()->createURL("supervise/default/update", array("id"=>$data->id))',
135 'imageUrl'=>Yii::app()->baseUrl.'/images/icons/user_edit.png',
136 ),
137 'delete'=>array(
138 'label'=>'删除',
139 'url'=>'Yii::app()->createURL("supervise/default/delete", array("id"=>$data->id))',
140 'imageUrl'=>Yii::app()->baseUrl.'/images/icons/user_delete.png',
141 ),
142 ),
143 ),

但是如果在别的地方你简单的使用 createUrl来创建的都是GET REQUEST,无法删除记录的

Error 400

Invalid request. Please do not repeat this request again.

解决方法:

 

16 <?php
17 echo CHtml::link(CHtml::encode('删除巡察记录'), array('/***/default/delete', 'id'=>$id),
18 array(
19 'submit'=>array('/***/default/delete', 'id'=>$id),
20 'class' => 'delete','confirm'=>'This will remove the image. Are you sure?'
21 )
22 );
23 ?>

转载请注明:PHP攻城师 

http://blog.csdn.net/phpgcs/article/details/10393305


 

 

    




原文地址:https://www.cnblogs.com/james1207/p/3285690.html