Cphalcon: Default value for an action parameter is ignored

Created on 5 Sep 2013  Â·  19Comments  Â·  Source: phalcon/cphalcon

Let's suppose I have a controller like this:

class IndexController extends ListController {

  public function indexAction($hero = 'superman') {
    echo $hero;
  }

}

When I get the http://mysite.com, without any parameters, Phalcon passes to the action an empty string instead of NULL. This implies the default parameter value is ignored.
To fix this behaviour you have to do this:

class IndexController extends ListController {

  public function indexAction($hero) {
    if (empty($hero))
      echo 'superman';
  }

}

Sometimes you want that http://mysite.com provide the same result of http://mysite.com/superman. Imagine for example a default order or results in a list.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

not a bug

All 19 comments

I test it ok, my code:

class IndexController extends ControllerBase
{

    public function indexAction($message = 'welcome')
    {
        echo $message;exit;
    }

}

Which version you are using? Whether the impact will be affected by ListController.

PHP 5.5.3 and Phalcon 1.2.3. Did you try to setup a parametric route for your IndexController? Because if you do, the default value for the $message parameter is overridden with an empty value. At least this happen to me.

I case of a parametric route I would say this is the expected behavior. PHP substitutes the default parameters only if corresponding arguments are not passed to the function/method, that is, if Phalcon invoked $controller->indexAction();

However, in the case of the parametrized route Phalcon passes the arguments it extracts from the query string and I think you need to specify your preferred defaults there.

So, if the argument is not passed at all, the default parameter is ignored. When the user doesn't pass a parameter Phalcon thinks, erroneously, the parameter value is an empty string, but it is not an empty string, is a null value. As you know in PHP null and empty are different things. That's the reason why the default value for the action parameter is ignored, because Phalcon passes, anyway, an empty string and not null. It's a bug, you can't say that it is the expected behavior.
Like title says: the default value for an action parameter is ignored. You can't provide a default value for your parameters because Phalcon will ignore them. I'm sorry I have to insist, but it's a bug. :-(

I add route this, test ok:

$di->set('router', function () {
    $router = new \Phalcon\Mvc\Router();
    $router->add(
        "/hello/:params",
        array(
            "controller" => "index",
            "action"     => "index",
            "params"     => 1
        )
    );
    return $router;
});

View http://localhost/hello, show

welcome

View http://localhost/hello/world, show

word

This instead doesn't work:

namespace Foo\Route;

use Phalcon\Mvc\Router\Group;

//! @brief Group of blog routes.
//! @nosubgrouping
class BlogGroup extends Group {

  public function initialize() {
    // Sets the default controller for the following routes.
    $this->setPaths(
      [
        'namespace' => 'Foo\Controller',
        'controller' => 'blog'
      ]);

    $this->addGet('/articles/{period}', ['action' => 'articles']);
  }

}

And the controller:

namespace Foo\Controller;

use Phalcon\Mvc\Router\Controller;

class BlogController extends Controller {

  public function articlesAction($period = 'week') {
    if (empty($period))
      echo 'period is empty';
  }

}

The output is:

period is empty

The parameter is optional and when is not provided it should get his default value, in this case week, instead due to a bug, Phalcon passes an empty string instead a null value to the function, so the default value is ignored, because PHP consider empty a string like this ''.

Even if Phalcon passes null value, nothing will change — fir PHP there is a difference whether the parameter is passed or not.

Consider this:

<?php

function hello($param = 'world')
{
        echo 'Hello, ', $param, "\n";
}

hello(null);

You right, even with null doesn't work, and that's right because when you call hello(null)you are still passing something. In case of an optional parameter, Phalcon shouldn't pass the parameter at all, when the parameter is not provided.

When http://example.com/articles/is requested Phalcon should invoke the method articlesAction() without parameters.

Does "Closed" means has been fixed?

This bug is still there, checked 1.3.0.

No it is not. Github automatically closed a lot of bugs when we had to rename one branch.

Opening it again

Thanks Nikolaos.

Well you closed this, but this is a bug.

  • No reaction since Aug 23, 2015, when issue was tagged as "not a bug"
  • We don't support 1.3.x very long ago

You right. Just to be sure, doesn't this problem affect 2.x?

I'm not sure. I'll open this issue again if the problem is present in 2.x

I will check and I'll let you know.

Phalcon 2 doesn't have this bug anymore, just checked.

Thanks

Was this page helpful?
0 / 5 - 0 ratings