2014-11-05 67 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'); 
    } 
} 

我希望这将有助于解决这一问题。