flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.6k stars 407 forks source link

Flightphp query param problem on GET method #416

Closed nikiedev closed 5 months ago

nikiedev commented 4 years ago

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?

masakik commented 4 years ago

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]);
geogkary commented 4 years ago

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
});