Slim4 GET query params from skeleton Action.args

Using Slim4 skeleton app

    // in routes.php
    return function (App $app) {
        $app->post('/api/public/test', TestAction::class);
    }

In TestAction.php

class TestAction extends Action {

    protected TestService $svcTest;

    public function __construct(LoggerInterface $logger, TestService $svcTest) {
        parent::__construct($logger);
        $this->svcTest = $svcTest;
    }

    protected function action(): Response {

        // this throws HttpNotFoundException
        $param1 = $this->resolveArg("param1"); 

        // this works, but why didn't Action __invoke() populate args?
        $param1 = $this->request->getQueryParams()['param1']; 
    }
...

Can you please add the link to the Skeleton project?

Its been a while, but I think I created my project like this:

$ composer create-project slim/slim-skeleton [my-app-name]

It should populate args, see here

// this throws HttpNotFoundException
$param1 = $this->resolveArg(“param1”);

This (HttpNotFoundException) indicates that there is an routing issue.

@odan

I’m mis-copy/pasted wrong exception. I’m getting HttpBadRequestException in the “resolveArg()” call.

class TestAction extends Action {

    protected function action(): Response {

        // this throws HttpBadRequestException
        $param1 = $this->resolveArg("param1"); 

        // this works, but why didn't Action __invoke() populate args?
        $param1 = $this->request->getQueryParams()['param1']; 
    }
    ...

and in Action.php

    ...
    protected function resolveArg(string $name) {
        if (!isset($this->args[$name])) {
            throw new HttpBadRequestException($this->request, "Could not resolve argument `{$name}`.");
        }

        return $this->args[$name];
    }

$param1 = $this->resolveArg(“param1”);

The resolveArg method returns the value from the route placeholder, but not the HTTP queryString as array. For example when you have a route like this:

$app->get('/hello/{param1}', ...

and the user requests a URL like: https://www.example.com/hello/myvalue
then $param1 = $this->resolveArg("param1"); will return the value myvalue.

To fetch a specific QuertyString value, you need to use the request object in this project as you already mentioned.

$param1 = $this->request->getQueryParams()['param1'];