Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete is not working Method Not Allowed (#405) gridview yii2

This is my gridview, I changed from class actionColumn to this:

[   'format' => 'html',
            'contentOptions'=>['style'=>'width: 5px;'],
            'value' => function($model) {
                if($model->id == Yii::$app->user->identity->id) {
                    return  Html::a('<i class="glyphicon glyphicon-share-alt"></i>').' '. 
                            Html::a('<i class="glyphicon glyphicon-pencil"></i>', ['update', 'id' => $model->id]).' '.
                            Html::a('<i class="glyphicon glyphicon-trash"></i>', ['delete', 'id' => $model->id], ['data' => ['confirm' => 'Do you really want to delete this element?','method' => 'post']]);
                }
                return '';
            },
        ],

Which gives me an error.

Method Not Allowed (#405)

Method Not Allowed. This url can only handle the following request methods: POST. 

When I changed to actionColumn again it was working, but I changed the code and it just gives me an error.

like image 272
Noobs To be Pro Addict Avatar asked Sep 02 '15 14:09

Noobs To be Pro Addict


People also ask

Why delete method is not allowed?

If your application uses . NET WebAPI, you may get a 405 Method Not Allowed error when you issue a PUT or DELETE request. This error is caused because the WebDAV module intercepted the request.

What causes a 405 error?

The 405 Method Not Allowed error occurs when the web server is configured in a way that does not allow you to perform a specific action for a particular URL. It's an HTTP response status code that indicates that the request method is known by the server but is not supported by the target resource.

How do I get a 405 postman error?

405 Method No Allowed means that you are not using the correct HTTP Method with that endpoint. For this specific case I'd say that the endpoints you are consuming do not accept the POST method.


2 Answers

Since the html formatter will clean the value using HtmlPurifier, you should simply change the format to raw .

Read more : http://www.yiiframework.com/doc-2.0/guide-output-formatter.html#other-formatters

like image 173
soju Avatar answered Sep 30 '22 14:09

soju


what you can also do is this set the buttons paramter in the actioncoloumn see http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$buttons-detail

e.g. something like this:

      'buttons' => [
            'update' => function ($url, $model, $key) {
                if ($model->id == Yii::$app->user->identity->id) {
                    $options = [
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                    ];
                    return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
                }
            },
            'delete' => function ($url, $model, $key) {
                if ($model->id == 6929) {
                    $options = [
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                    ];
                    return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
                }
            },
        ],

But i would just extend my own class from the ActionColumn Class create a function like this. I understand your code that all buttons should be hidden or displayed depending if the model->id is the user->identity->id

protected function renderDataCellContent($model, $key, $index)
{
    if ($model->id == Yii::$app->user->identity->id) {
        return parent::renderDataCellContent($model, $key, $index);
    }
}

Hope this helps. I Would use the approach with the extended Actioncolumn class. Because then all links are still working if e.g. the urlCreator function is changed or pjax is enabled for the grid etc.

Nonetheless the reason why the post request wasn't working was correct as soju has written above.

like image 39
BHoft Avatar answered Sep 30 '22 16:09

BHoft