簡體   English   中英

ZF2 Restful API不路由選項方法

[英]ZF2 Restful API not routing options method

我正在通過向其發送OPTIONS命令來測試我的zf2 restful api,但它會直接進入路由器中定義的操作中,而不是options()方法中。

路由器:

'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;
    }
}

當我向/ api / some / execute發送一個OPTIONS命令時,它直接進入執行動作,而不是進入options方法。 路由中我缺少什么嗎? 我認為發送任何OPTIONS命令會將其路由到options()。

謝謝

因此,似乎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;
    }

希望這對以后的人有所幫助。

我遇到了這個問題,並按以下方式解決:

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');
    }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM