• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

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


 

 


 

    




 

posted @ 2013-08-27 18:47  Class Xman  阅读(422)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3