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

Have PhalconPHP accepts params as $_GET key/value pairs

Hi,

I would like Phalcon to pass parameters in the URL as GET values, e.g:

https://localhost/www/phalconproject/tests/index/name/dew/id/28

would trigger the indexAction funciton in the TestsController.php, and pass: name => dew id => 29

as get values. Is this possible?

I am migrating to Phalcon from Yii, where this was standard, which is why I need it. I tried figuring it out with the Routers documentation, but could'nt figure it out.

Thanks for your help! Alex



103
Accepted
answer
edited Sep '14

Hi Alex,

You can access those values in the controller in two ways:

Using the default way: https://localhost/www/phalconproject/tests/index/dew/28

public function indexAction($name, $id)
{
    echo $name;
    echo $id;

}

Using the request parameter : https://localhost/www/phalconproject/tests/?name=dew&id=29

public function indexAction()
{
    //Make sure to apply filters accordingly
    $name = $this->request->getQuery("name");
    $id = $this->request->getQuery("id");
}

More info here: https://docs.phalcon.io/en/latest/reference/controllers.html#



5.7k
edited Sep '14

Thanks Dan!

That would indeed work, but the problem with solution 1 is, that you have to pass the params in the precise order. The clients that consume the API, do not neccesarily put these in the same orders.

Solution 2 would tackle that, but then all those clients consuming the API would need to be rewritten, they would have to use instead of:

/id/28/name/dew/pass/12345/confirm/12345

?id=28&name=dew&pass=12345&confirm=12345

...which is why I am looking for a way to have Phalcon deal with this...to not have to force all those client changes. I appreciate your help though, hope that there is an easy 'hack' / workaround to accomplish this.



5.7k
edited Sep '14

Wait, I found the solution, thanks to the link Dan provided:

public function showAction()
    {
        $year = $this->dispatcher->getParam('year');
        $postTitle = $this->dispatcher->getParam('postTitle');
    }

So using the dispatcher allows you to capture the values. I am marking his answer as the accepted one, as he pointed me to the solution.



5.7k

Actually I cheered too early.

It looks like one needs to hack into the Phalcon\Mvc\Dispatcher exactly how it is described here: https://docs.pir.kr/reference/dispatching.html#preparing-parameters

As I am a total newbie, where does this code go? In my custom BaseController that extends Phalcon\Mvc\Controller?

Thanks for anyone's help. Alex



98.9k
Accepted
answer