Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Not Allowed on $this->request->allowMethod(['post', 'delete']);

I was following this tutroial and everything is working fine, except the Deleting Articles. When I'm calling delete method:

public function delete($id)
    {
        $this->request->allowMethod(['post', 'delete']);

        $job = $this->Jobs->get($id);
        if ($this->Jobs->delete($job)) {
            $this->Flash->success(__('Job: {0} has been deleted.', h($job->name)));
            return $this->redirect(['action' => 'index']);
        }
    }

I have this error:

Method Not Allowed: $this->request->allowMethod(['post', 'delete']);

But if i remove this line from my method, it works:

$this->request->allowMethod(['post', 'delete']);

So what am I doing wrong, and what I'm missing, why in the tutorial it works with this, but in my case it doesn't. Should I additionally allow something?

Here is php code of the action:

echo $this->Html->link('Delete',    array('controller' => 'Jobs','action' => 'delete', $job->id),
                                    array('confirm' => 'Are You Sure?','class' => 'button red'));

Here is html code of the action:

<a href="/jobsfind/jobs/delete/32" class="button red" onclick="if (confirm(&quot;Are You Sure?&quot;)) { return true; } return false;">Delete</a>
like image 336
Tachi Avatar asked Apr 10 '26 09:04

Tachi


1 Answers

The problem, that Your request is GET and not DELETE. So, You should use AJAX and define type of Your request:

$.ajax({
  type : 'DELETE',
  data : 'post_id=' + post_id,
  success : function (response) {
    console.log(response);
  }
})

This piece of code might help. If no - just use GET request to remove, because POST request commonly using for adding data.

Or You should provide hash with tokens that will be checking in Middleware.

like image 200
Roman Kozin Avatar answered Apr 12 '26 23:04

Roman Kozin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!