xperseguers / t3ext-routing

TYPO3 Extension routing
19 stars 6 forks source link

[FEATURE] add OPTIONS http method #26

Closed AlexKvrlp closed 5 years ago

AlexKvrlp commented 5 years ago

HTTP OPTIONS will enable controller to handle ajax preflights

Howto handle preflights:

  1. Add route
  2. Implement controller action and preflight

Add route

-
  name: 'preflight foo route'
  uriPattern: 'some/uri'
  defaults:
    '@package':    'SOME.package'
    '@plugin':     'default'
    '@controller': 'Rest'
    '@action':     'preflightFoo'
  httpMethods: ['OPTIONS']
-
  name: 'foo route'
  uriPattern: 'some/uri'
  defaults:
    '@package':    'SOME.package'
    '@plugin':     'default'
    '@controller': 'Rest'
    '@action':     'foo'
  httpMethods: ['GET']

Implement controller action and preflight

    /**
     * preflight action
     */
    public function preflightFooAction()
    {
               #Doing some stuff to handle the preflight
               $this->controllerContext->getResponse()->setHeader('Access-Control-Allow-Origin', 'https://api.example.com);
               $this->controllerContext->getResponse()->setHeader('Access-Control-Allow-Headers', 'Authorization');

               #return empty response
               #firefox only accept repsonse with StatusCode 200 NOT 204 in my case
    }

    /**
     * foo action
     */
    public function fooAction()
    {
    }
xperseguers commented 5 years ago

Next time please stick to proper commit message format.

AlexKvrlp commented 5 years ago

I'll promise