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

Passing agruments to Controller action using router short form.

I've run to a very basic problem, and that might because I didn't do enough research, but I will ask it anyway.

In my Controller, let's say it's like this:

class EchoController extends ControllerBase {
     public function sayAction($str = "Hello") 
     {
          echo $str;
     }

Now in routes.php file I have some thing like this

$router->add('/say/:params', array(
    'controller'    => 'Echo',
    'action'        => 'say',
    'params'        => 1,
));

and all could be good, but the document here (https://docs.phalcon.io/en/latest/reference/routing.html#mixing-array-and-short-syntax) said that I could also mixing with short form, with I always find more pleasant to type in.

I could also use short syntax like $route->add('say/{something}', 'Echo::say'); and then call $this->dispatcher->getParam('something') in the controller, but I prefer to map the params directly to the function arguments, because then I could use the default values that way (and it's clearer).

Any suggest?

Besides, is there anyway to directly map the route parameters to function arguments instead of having to call $this->dispatcher->getParam() all the time?

edit: try modify the code around, and the syntax $route->add('say/{str} actually map the str parameter to variable $str inside the controller, but it still cant make advance of the default values.

Is there anyway to the route behaviour like in Laravel, allowing a URI segment to be optional?



11.8k
edited Oct '14

add exit(); in you Action and you will see your string. I guess, it feature Phalcon MVC

public function sayAction($str = "Hello") 
     {
          echo $str;
          exit();
          ///or  exit($str);
     }