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

$_GET parameters as action parameters

Is there a way to pass $_GET parameters og HTTP request to action ?

$_GET['id'] =>

public function indexAction($id)
{
}


98.9k

You can prepare the dispatcher arguments before pass them to the dispatched controller/action:

<?php

use Phalcon\Dispatcher,
    Phalcon\Mvc\Dispatcher as MvcDispatcher,
    Phalcon\Events\Manager as EventsManager;

$di->set('dispatcher', function() {

    //Create an EventsManager
    $eventsManager = new EventsManager();

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

        $keyParams = array();
        foreach ($_GET as $key => $value) {
            $keyParams[$key] = $value;
        }

        //Override parameters
        $dispatcher->setParams($keyParams);
    });

    $dispatcher = new MvcDispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

https://docs.phalcon.io/en/latest/reference/dispatching.html#preparing-parameters

It works, but action parametrs does not have same names as $_GET keys, it only write params in asc order. For example:

request string: index.php?_url=/index/help&pid=5&id=10

public function helpAction($id, $lol) { // $id = /index/help // $lol = 5 // }



98.9k

You have to obtain a ReflectionMethod of the method executed like the one at the link I posted before in the example of the documentation to pass the items in the right order.