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

Mounting a custom route group not working

I extended the default Phalcon Router with a new way to mount groups. This enables me to mount groups using a different kind of syntax. It has the function below:

public function group($prefix = null, $routes) {
    $group = new RouterGroup();
    if ($prefix != null) {
        if (strpos($prefix, '/') == false) {
            $prefix = '/'.$prefix;
        }
        $group->setPrefix($prefix);
    }

    $routes($group);

    $this->mount($group);

    return $group;
}

Using this, I can add a new route group like this:

$router = new Router();

$router->removeExtraSlashes(true);

$router->group("api", function ($group) {
});

On my local machine, running Phalcon 3.2 on PHP 5.6.3, this works perfectly. However, on a server, running Phalcon 3.2 on PHP 7, it doesn't. It started throwing an error. Warning: Second argument is not an array in Router.php on line 48. That's an error in $this->mount($group);. Any ideas as to what could be going wrong?

edited Aug '17

What is your OS and web server configuration used for development?

What var_dump($routes) is showing in your method:

group($prefix = null, $routes)

?

Try to type cast it as string on PHP7, just for test, and of course supply string value then instead of anonymous function.

Bottomline: you should be running same major PHP versions on both dev and production environment.

I'm using macOS 10.12 with Phalcon 3.2 on PHP 5.6.3 running in MAMP 4.

var_dump($routes) shows me the closure. var_dump($routes($groups)) shows NULL, because the function does not return anything. var_dump($groups) shows me the routes as they should be as a RouterGroup object.

I don't see how changing it to a string would solve anything, because I need the routes in there and that's what's not working.

What is your OS and web server configuration used for development?

What var_dump($routes) is showing in your method:

group($prefix = null, $routes)

?

Try to type cast it as string on PHP7, just for test, and of course supply string value then instead of anonymous function.

Bottomline: you should be running same major PHP versions on both dev and production environment.

edited Aug '17

Uh-oh. I'm not an expert with MacOS. All I know they have miserably switched to Windows-like behavior when it comes to file system case sensitivity, enough not to consider the platform good choice for web development.

I said only for test type cast it as string. To see whenever Warning: Second argument is not an array would go away. Error is obvious, for some reason it expects array as second argument.

But it does not matter as var_dump($routes($groups)) -> gives you NULL? While both $routes and $groups variables are set and visible in your method?

Are you using full MVC app or Micro app?

Last but not least, try this:

//$this->mount($group); //instead of
$router->mount($group); //try this (or whatever object name you have while extending Router)

Well, what's on the line: Warning: Second argument is not an array in Router.php on line 48 Otherwise it's guessing in the dark.

edited Aug '17

@janisbizkovskis: OP already posted what is on that line: $this->mount($group);

It looks like $group->getRoutes() is not an array under php7?

@stamster, sorry, missed that.

Well, I would like to keep it simple - I don't see the second argument mentioned in message Warning: Second argument is not an array in Router.php on line 48. Have you overwritten your mount method as well?

This errors is from array_merge from zephir code most likely, i guess on php 5.x in Group somehow array in initialized in zephir, on php 7 not, just mount it after adding any routes :)