Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'OPTIONS' method when attaching a resource (REST) #45

Closed
alkavan opened this issue May 4, 2014 · 4 comments
Closed

Comments

@alkavan
Copy link

alkavan commented May 4, 2014

While building a RESTful API using router's attachResource() function I found myself adding a special case for the OPTIONS method as it's not supported by default. It looks something like this:

$router->attachResource('user', '/user');
$router->add('user.options', '/user')
    ->addServer(['REQUEST_METHOD' => 'OPTIONS'])
    ->addValues(['controller' => 'user', 'action' => 'options']);

Then in the controller it's simply:

public function options() {
    $this->response->content->set('*');
}

As you can see, I give a response telling the client all methods are allowed. but the reason I had to add this is because for example: Backbone.js by default will first check this method before making any other REST operations with it's models. you can "hack" you way around it in Backbone. but it does not look pretty and actually, why not adding OPTIONS as it's essential part of RESTful services.

I would be glad taking this task on myself, what do you think? does this sound reasonable or am missing something?

@harikt
Copy link
Member

harikt commented May 5, 2014

if I understood you correctly you are looking for REQUEST_METHOD OPTIONS . If that is correct use addServer , not addTokens

$router->add('user.options', '/user')
    ->addServer(['REQUEST_METHOD' => 'OPTIONS'])
    ->addValues(['controller' => 'user', 'action' => 'options']);

https://github.com/auraphp/Aura.Router/blob/develop-2/src/RouteCollection.php#L242

@alkavan
Copy link
Author

alkavan commented May 8, 2014

Yes, thanks, this what I mean. so I ended up with:

$router->addOptions('user.options', '/user')
    ->addValues(['controller' => 'user', 'action' => 'options']);

this indeed looks more reasonable now. still it's a bit strange I need to handle OPTIONS method here, other methods handled by attachResource().

@harikt
Copy link
Member

harikt commented May 8, 2014

oh so what I understood now is you are pointing that in attachResource, the options method is missing. Is that correct ?

@harikt
Copy link
Member

harikt commented May 9, 2014

If that is the case I think you can send a PR and discuss with @pmjones . ( The code that do the attachResource stuff is at https://github.com/auraphp/Aura.Router/blob/develop-2/src/RouteCollection.php#L460 )

pmjones pushed a commit that referenced this issue Jun 30, 2014
add 'options' in attachResource(); fixes #45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants