miladrahimi / phprouter

PhpRouter is a full-featured yet very fast HTTP URL router for PHP projects
MIT License
191 stars 17 forks source link

Obtaining QueryString Data #35

Closed mbamber1986 closed 1 year ago

mbamber1986 commented 3 years ago

cant see it in your documentation but does the routing support using ?keyword=mike im trying to setup a search page for users and it doesnt seem to be picking up and results to the catch and gives my custom error page which is just page not found

my form path is : /admin/search and then as a get method throws up ?keyword=

my route at the moment as i thought it might work is /admin/search/?keywork={keyword}

tia

miladrahimi commented 3 years ago

Hello, The ?keyword=mike and everything after ? is called Query String and is not part of the route. As for your search route /admin/search?keywork={keyword}, the route should be /admin/search and it can be mapped to controller like SearchController::index(). To catch the query string (keywork={keyword}) you can do something like the following code:

use Laminas\Diactoros\ServerRequest;

class SearchController {
    function index(ServerRequest $request) {
        $keyword = $request->getQueryParams()['keyword'];
        // Search $keyword in database or anywhere else...
        // return response as view or json, etc.
    }
}
mbamber1986 commented 3 years ago

Great thank you I will look at that later

On Fri, 12 Mar 2021, 07:23 Milad Rahimi, @.***> wrote:

Hello, The ?keyword=mike and everything after ? is called Query String and is not part of route. As for your search route /admin/search/?keywork={keyword}, the route should be /admin/search and it can be mapped to controller like SearchController::index(). To catch the query string (keywork={keyword}) you can do something like the following code:

use Laminas\Diactoros\ServerRequest; class SearchController { function index(ServerRequest $request) { $keyword = $request->getQueryParams()['keyword']; // Search $keyword in database or anywhere else... // return response as view or json, etc. } }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/miladrahimi/phprouter/issues/35#issuecomment-797291946, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHKAGFHJOK3C7CSAOKU4THLTDG6QLANCNFSM4YZU2KJQ .

mbamber1986 commented 1 year ago

Hi So as of now i am still using your Router ive been out of it for a while however it seems that $keyword = $request->getQueryParams()['keyword']; this no longer works ansd gives Undefined array key "id" , i have changed the word keyword to id.

i have tried wrapping it in an iseset and nothing seems to be changing has something changed on yours or laminas side for this to not work anymore

miladrahimi commented 1 year ago

what do u get when print_r($request->getQueryParams()) ?

mbamber1986 commented 1 year ago

image

heres the address bar snippet image

and my code

image