问题
In a yii site I am creating I have a page with the url http://localhost/administrator/restaurant/list
which shows the list of restaurants in a tabular format along with a delete button. The delete button points to http://localhost/administrator/restaurant/delete/<id>
.
The actionDelete
of my controller is as follows :
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);
}
But on clicking the delete button, the row is getting deleted successfully from the database but instead of redirecting to http://localhost/administrator/restaurant/list
the page is redirecting to http://localhost/administrator/restaurant/delete/restaurant/list
and showing an error. Is there something wrong with the way I implemented the redirect function ?
回答1:
Use array routing instead:
$this->redirect(array('restaurant/list'), true);
Using GET for deleting is a very bad idea because browsers can prefetch links before you even click them. You should use POST for any scenario like this.
来源:https://stackoverflow.com/questions/10671477/yii-not-redirecting-properly-with-the-ccontrollerredirect-function