2014-11-05 136 views
0

我正在通過向它發送一個OPTIONS命令來測試我的zf2 restful api,但它直接進入在路由器中定義的動作而不是options()方法。ZF2 Restful API非路由選項方法

路由器:

'router' => array(
    'routes' => array(
     'edatafeed' => array(
      'type' => 'Zend\Mvc\Router\Http\Literal', 
      'options' => array(
       'route' => '/api', 
       'defaults' => array(
        '__NAMESPACE__' => 'Application\Controller', 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'default' => array(
        'type' => 'Segment', 
        'options' => array(
         'route' => '/:controller[/:action][/]', 
         'constraints' => array(
          'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         ), 
         'defaults' => array(
         ), 
        ), 
       ), 
      ), 
     ), 
    ), 
), 

控制器:

class SomeController extends ApiController 
{ 
    protected $dm; 

    private function getDm() 
    { 
     $this->dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default'); 
    } 

    public function executeAction() 
    { 
     return new JsonModel(array(
      'ok' => false, 
      'data' => null, 
     )); 
    } 
} 

ApiController:

class ApiController extends AbstractRestfulController 
{ 

    protected function methodNotAllowed() 
    { 
     $this->response->setStatusCode(405); 
     throw new \Exception('Method Not Allowed'); 
    } 

    public function options() 
    { 
     $response = $this->getResponse(); 
     $headers = $response->getHeaders(); 

     $headers->addHeaderLine('Allow', implode(',', array(
      'GET', 
      'POST', 
     ))) 
     ->addHeaderLine('Content-Type','application/json; charset=utf-8'); 
     return $response; 
    } 
} 

當我發送的OPTIONS命令/ API /一些/執行它進入右入執行行動,而不是進入期權方法。在路由中是否有我缺少的東西?我認爲發送任何OPTIONS命令會將它路由到options()。

感謝

回答

0

如此看來,在AbstractRestfulController實際檢查,看是否請求是一個自定義操作檢查請求的方法類型之前。因此,由於我定義了一個名爲「execute」的動作,它會在檢查其是否爲OPTIONS命令之前直接路由到executeAction()。我不得不重寫我的ApiController類中的onDispatch()方法,並且只有路由到我的自定義方法(如果它的GET,POST或UPDATE命令)。否則,路由到適當的方法類型方法。

這是我修改了代碼:

// RESTful methods 
    $method = strtolower($request->getMethod()); 

    // Was an "action" requested? 
    $action = $routeMatch->getParam('action', false); 
    if ($action && 
     ($method == 'get' || $method == 'post' || $method == 'update')) { 
     // Handle arbitrary methods, ending in Action 
     $method = static::getMethodFromAction($action); 
     if (! method_exists($this, $method)) { 
      $method = 'notFoundAction'; 
     } 
     $return = $this->$method(); 
     $e->setResult($return); 
     return $return; 
    } 

希望這有助於在未來別人。

0

我有這個問題,解決這個問題如下:

class ApiRestfullController extends AbstractRestfulController { 

    protected $allowedCollectionMethods = array(
     'POST', 
     'GET' 
    ); 

    public function setEventManager(EventManagerInterface $events) { 
     parent::setEventManager($events); 

     $events->attach('dispatch', function ($e) { 
      $this->checkOptions($e); 
     }, 10); 
    } 

    public function checkOptions($e) { 
     $response = $this->getResponse(); 
     $request = $e->getRequest(); 
     $method = $request->getMethod(); 

     if (!in_array($method, $this->allowedCollectionMethods)) { 
      $this->methodNotAllowed(); 
      return $response; 
     } 
    } 

    public function methodNotAllowed() { 
     $this->response->setStatusCode(
      \Zend\Http\PhpEnvironment\Response::STATUS_CODE_405 
     ); 
     throw new Exception('Method Not Allowed'); 
    } 
} 

我希望這將有助於解決這一問題。