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

Phalcon\Router without action

Hello everyone,

again I got a little question. Currently I'm trying to implement a RESTful service. I already implemented a rest controller that redirect for each type (get, post etc.). Now I got a problem with my router.

Here is my router:

$router = new Phalcon\Mvc\Router(false);
$router->add("/", array('module' => 'core', 'controller' => 'index','action' => 'get')); // works
$router->add('/:module', array('module' => 1)); // works
$router->add('/:module/:controller', array('module' => 1,'controller' => 2)); // works
$router->add('/:module/:controller/:params', array('module' => 1, 'controller' => 2, 'params' => 3)); // works not :(
$router->notFound(array('module' => 'core', 'controller' => 'index', 'action' => 'error')); // works
$router->setDefaults(array('module' => 'core', 'controller' => 'index', 'action' => 'index')); // works

Does anybody now, why this here: /:module/:controller/:params not works properly? Is it mandatory to set an action?

Thanks!



98.9k

Create a test file like this, to check how your routes are being handled:

$router = new Phalcon\Mvc\Router(false);

$router->add("/", array(
    'module' => 'core',
    'controller' => 'index',
    'action' => 'get'
)); // works

$router->add('/:module', array(
    'module' => 1
)); // works

$router->add('/:module/:controller', array(
    'module' => 1,
    'controller' => 2
)); // works

$router->add('/:module/:controller/:params', array(
    'module' => 1,
    'controller' => 2,
    'params' => 3
)); // works not :(

$router->notFound(array(
    'module' => 'core',
    'controller' => 'index',
    'action' => 'error'
)); // works

$router->setDefaults(array(
    'module' => 'core',
    'controller' => 'index',
    'action' => 'index'
)); // works

$tests = array(
    '/',
    '/module/controller',
    '/module/controller/p1',
    '/module/controller/p1/p2'
);

foreach ($tests as $test) {

    $router->handle($test);
    echo 'Route = ', $test, '<br>';
    echo 'Module = ', $router->getModuleName(), '<br>';
    echo 'Controller = ', $router->getControllerName(), '<br>';
    echo 'Action = ', $router->getActionName(), '<br>';
    echo 'Parameters = ', join(', ', $router->getParams()), '<br>';
    echo '<br>';
}

The above snippet produces:

Route = /
Module = core
Controller = index
Action = get
Parameters = 

Route = /module/controller
Module = module
Controller = controller
Action = index
Parameters = 

Route = /module/controller/p1
Module = module
Controller = controller
Action = index
Parameters = p1

Route = /module/controller/p1/p2
Module = module
Controller = controller
Action = index
Parameters = p1, p2

Ok thanks. This works really :)

I see this needs a workaround that it worked like:

// URL https://example.com/test/any/paramKey/paramValue/
array(
   'module' => 'test',
   'controller' => 'any',
   'params' => array(
       'paramKey' => 'paramValue'
   )
)

If someone is interested, this is my current fast workaround:

            $params = $this->getDI()->get('router')->getParams();
            $lastKey = null;
            foreach($params as $paramKey => $param){
                if($paramKey%2 === 0){
                    $lastKey = $param;
                }else{
                    $this->_get[$lastKey] = $param;
                }
            }
            $this->_get = array_replace_recursive($this->_get, $_GET);

Thanks for the info!