Welcome Guest, Not a member yet? Register   Sign In
RESTful API Support
#11

Sure, I'd like to help. And yeah, I can share. That's the whole point. I had something more elaborate (e.g. color coded) in mind, but had to make do with that. So it might need some polishing.

I've created a repo for the form data parser and included a usage example. I'm using almost the same code in my app. I haven't written any description yet, but will soon.

As a side note, I've encountered more problems regarding the REST. I can't seem to use match() in a group() in routes. The problem is empty($from) check in the match() method. I had to remove it to use match() in a group().

Now, the actual problem. Why would I use match in a group? Let's say I have an /admins endpoint. For the collection, I have three methods that I support: OPTIONS, GET, and POST. If someone makes a PUT request to /admins, the app should return 405 Method Not Allowed. Currently, it throws an error, saying

> Fatal error: Uncaught CodeIgniter\Exceptions\PageNotFoundException: Can't find a route for 'admins'

To fix this, I have to manually add routes to capture methods that are not supported. For example:

PHP Code:
$routes->options("admins""Admins::adminsOptions" );
$routes->get(    "admins""Admins::getAdmins"     );
$routes->post(   "admins""Admins::addAdmin"      );

$routes->match(["put""patch"], "admins", function () {
    echo "405 Method Not Allowed";
}); 

because I'm using group, the same can be written as:

PHP Code:
$routes->group("admins", function ($routes) {
    $routes->options("""Admins::adminsOptions" );
    $routes->get(    """Admins::getAdmins"     );
    $routes->post(   """Admins::addAdmin"      );

    $routes->match(["put""patch"], "", function () {
        echo "405 Method Not Allowed";
    });
}); 

and here, we come accross the problem I've mentioned above. Using match in group doesn't work, because the $from parameter is set to "".

I think there should be an easier way to deal with unexpected/unsupported methods.

So I inspected the $route property in RoutesCollection. This is how it looks when it's populated:

PHP Code:
'options' => [
    "admins" => [
        "route" => [
            "admins" => "\App\Controllers\Admins::adminsOptions"
        ]
    ]
],
'get' => [
    "admins" => [
        "route" => [
            "admins" => "\App\Controllers\Admins::getAdmins"
        ]
    ]
],
'post' => [
    "admins" => [
        "route" => [
            "admins" => "\App\Controllers\Admins::addAdmin"
        ]
    ]
], 

Is grouping the routes by methods intentional? Is there a benefit to this? I feel like this approach is backwards. I mean the endpoint/URI is fragmented into multiple methods. This doesn't look practical. How would one deal with unexpected/not-allowed methods in this format?

If it were like the following, I think it'd be more practical. First, I match the URI "admins", and then the method. If the URI matches, but the method doesn't, I'd return the 405 Method Not Allowed status.

PHP Code:
"admins" => [
    "options" => "\App\Controllers\Admins::adminsOptions",
    "get"     => "\App\Controllers\Admins::getAdmins",
    "post"    => "\App\Controllers\Admins::addAdmin",
], 

So, prioritizing URIs over methods, especially in regard to REST, seems like a better choice.

These are my superficial observations, so there might be things that I'm overlooking. I just thought I should share.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB