core icon indicating copy to clipboard operation
core copied to clipboard

Flightphp query param problem on GET method

Open nikiedev opened this issue 5 years ago • 2 comments

Hi! I am sending data from Controller to View via GET|POST settings in router. Well, here is my Router:

$admin = new AdminController();
Flight::route('GET|POST /admin/articles', [$admin, 'articles']);
Flight::route('GET /admin/articles/@id', [$admin, 'articles']);

as you see, i have a method in controller:

public function articles($id = null)
{
  // ...
}

When i make query like this: /admin/articles?id=1 all works fine, but when the query looks like the next one: /admin/articles/1 i don't get data from Flight::request()->query Can someone help me please to solve the problem?

I made a temporary solution (in htaccess): RewriteRule ^admin/articles/(\w+)$ /admin/articles?id=$1 but, may be there is a better way?

nikiedev avatar Mar 30 '20 21:03 nikiedev

The way I found to solve this is as follow:

$admin = new AdminController();
Flight::route('GET /admin/articles/@id', function($id) use ($admin) {
    $admin->articles($id);
});

But seems to be useful if there is a way to do it directly. Maybe

Flight::route('GET /admin/articles/@id', [$admin, 'articles', $id]);

masakik avatar Mar 31 '20 18:03 masakik

Flight collects queries only by the standard format (?query=example). Anything else used in the URI in the standard format (ex. admin/articles/1) is part of the route.

So if you need it as an argument in the route, use @masakik's solution above.

I'd suggest you simplify it though:

// notice the (/@id) which allows you to run all routes here
Flight::route('GET|POST /admin/articles(/@id)', function($id) {
    $admin = new AdminController();
    $admin->articles($id); // id = X || null
});

geogkary avatar May 02 '20 13:05 geogkary