miladrahimi / phprouter

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

Passsing custom Query String #54

Closed mbamber1986 closed 3 weeks ago

mbamber1986 commented 1 month ago

Hi, i havent made an issue in a while however i have come across a wierd bug and hoping you can possibly assist or fix it.

the issue im having is i have created a custom requests class which works without the router in place however when i use the router and try and load my request class it doesnt display unless the root path is /

for example if i use /users/?username=martin : this displays nothing

where as if i make the url /?username=martin it outputs the value of $username

is this normal behaviour or does it need fixing.

HOMECONTROLLER OUTPUTUSERS router

mbamber1986 commented 1 month ago

Just a quick follow up the my post request Actually passes through just seems to be query stings, im assuming this is because you can pass the Query string into the url with the router anyway hope to hear from you soon

miladrahimi commented 3 weeks ago

Hi @mbamber1986, you get query strings in your controllers using:

$request->getQueryParams();
mbamber1986 commented 3 weeks ago

I can do that if the path is / but anything after that it doesn't display.

Is there something in doing wrong?

My aim was to pass query strings on specific paths, not a major issue

ie /users/login?attempt=1 2 3 etc of 3 lock account.

really my end goal was api request but i can probably pass them through the router {param1} options

miladrahimi commented 3 weeks ago

Using the latest verison of PhpRouter.

<?php

require "../vendor/autoload.php";

class MyController
{
    public function page(\Laminas\Diactoros\ServerRequest  $request)
    {
        return json_encode($request->getQueryParams());
    }
}

$r = \MiladRahimi\PhpRouter\Router::create();
$r->get('/', function () { return 'home'; });
$r->get('/page', [MyController::class, 'page']);
$r->dispatch();

And run using:

php -S localhost:4242

Worked as shown the image below.

Screenshot 2024-08-17 at 12 12 24