Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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:

http://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: http://localhost/www/phalconproject/tests/index/dew/28

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

}

Using the request parameter : http://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: http://docs.phalconphp.com/en/latest/reference/controllers.html#



3.0k
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.



3.0k
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.



3.0k

Actually I cheered too early.

It looks like one needs to hack into the Phalcon\Mvc\Dispatcher exactly how it is described here: http://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



82.7k
Accepted
answer