Closed Salamek closed 7 years ago
@Salamek I'm not using CrudRouter but ResourceRoute which is parent. You can pass OPTIONS as a flag (third parameter)
$restApi[] = new CrudRoute('api/1.0/<module>/<presenter>[/<id>]', 'User', CrudRoute::CRUD | CrudRoute::OPTIONS);
If that will work for you, it can be better solution than rewriting extension.
@marten-cz that wont work, cos ResourceRoute::$actionDictionary is overwriten in CrudRoute after parent::__construct():
public function __construct($mask, $metadata = array(), $flags = IResourceRouter::CRUD)
{
if (is_string($metadata) && count(explode(':', $metadata)) === 1) {
$metadata .= ':default';
}
parent::__construct($mask, $metadata, $flags);
$this->actionDictionary = array(
IResourceRouter::POST => self::ACTION_CREATE,
IResourceRouter::GET => self::ACTION_READ,
IResourceRouter::PUT => self::ACTION_UPDATE,
IResourceRouter::PATCH => self::ACTION_PATCH,
IResourceRouter::DELETE => self::ACTION_DELETE
);
}
i implemented my own CrudRoute and changed it to this to make it work properly:
public function __construct($mask, $metadata = array(), $flags = IResourceRouter::CRUD)
{
if (is_string($metadata) && count(explode(':', $metadata)) === 1) {
$metadata .= ':default';
}
parent::__construct($mask, $metadata, $flags);
$actions = array(
IResourceRouter::POST => self::ACTION_CREATE,
IResourceRouter::GET => self::ACTION_READ,
IResourceRouter::PUT => self::ACTION_UPDATE,
IResourceRouter::PATCH => self::ACTION_PATCH,
IResourceRouter::DELETE => self::ACTION_DELETE,
);
foreach($actions AS $resource => $action)
{
$this->actionDictionary[$resource] = $action;
}
}
I will create PR if it passes unittests
Hi i have simple CrudRoute API
Router:
Everything works as expected, but when i try to call
OPTIONS
on any REST url (AJAX call do that from node) i get "Method not supported. Available methods: POST, GET, PUT, PATCH, DELETE". But it should return only header (200 code) with Allowed options for specified url (POST, GET, PUT, PATCH, DELETE for Crud) like this:Some doc to OPTIONS https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
I had to ughly hotfix it for now by modifying (deadline is ... passed 14days ago, i dont have time to implement this properly and send PR, sorry):
PS: and would be great to have option to configure
Access-Control-Allow
headers somewhere... so we can call rest api from JS directly (JSONP is not a good solution when we can use pure AJAX)