We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

PhalconPHP - match GET parameters in Action

I have a problem with PhalconPHP that it's driving me crazy.

I'm developing an API, the URL is: ..../api/users/getAll?user_id=1&email=aaa

This bring me to Controller: UsersController, and action: getAllAction($user_id, $email). (OK).

My beforeDispatch event, prepare action parameters:

// Attach a listener
$eventsManager->attach(
    "dispatch:beforeDispatchLoop",
    function (Event $event, $dispatcher) {

        $request = new Request();
        $params = $request->get();
        $keyParams = [];

        // Use odd parameters as keys and even as values
        foreach ($params as $key => $value) {
            if ($key == '_url') continue;

            $keyParams[$key] = $value;
        }
        // Override parameters
        $dispatcher->setParams($keyParams);
    }
);

When I call URL in this order: URL?user_id=aa&email=bbb everything is OK, but when i call URL with inverted parameters, (URL?email=bbb&user_id=aaa), getAllAction matchs parameters wrongly.

Doubt: now action parameters are matching by position, but i need to change it by name.

It's posible?

PD: I don't want to use $request->getQuery or $request->get. I want to match dynamically by name.

Any idea?

Thank you!

Instead of injecting params with an event listener / dispatcher, why don't you simply define route instead of: ..../api/users/getAll?user_id=1&email=aaa ... ..../api/users/getAll/{userID}/{email}

https://docs.phalcon.io/en/latest/reference/routing.html#defining-routes