CakeRequest::allowMethod PHP Method

allowMethod() public method

Example: $this->request->allowMethod('post', 'delete'); or $this->request->allowMethod(array('post', 'delete')); If the request would be GET, response header "Allow: POST, DELETE" will be set and a 405 error will be returned.
public allowMethod ( string | array $methods ) : boolean
$methods string | array Allowed HTTP request methods.
return boolean true
    public function allowMethod($methods)
    {
        if (!is_array($methods)) {
            $methods = func_get_args();
        }
        foreach ($methods as $method) {
            if ($this->is($method)) {
                return true;
            }
        }
        $allowed = strtoupper(implode(', ', $methods));
        $e = new MethodNotAllowedException();
        $e->responseHeader('Allow', $allowed);
        throw $e;
    }

Usage Example

 /**
  * Test allowMethod throwing exception
  *
  * @return void
  */
 public function testAllowMethodException()
 {
     $_SERVER['REQUEST_METHOD'] = 'PUT';
     $request = new CakeRequest('/posts/edit/1');
     try {
         $request->allowMethod('POST', 'DELETE');
         $this->fail('An expected exception has not been raised.');
     } catch (MethodNotAllowedException $e) {
         $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
     }
     $this->setExpectedException('MethodNotAllowedException');
     $request->allowMethod('POST');
 }