We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

default index action not working anymore in 1.3 Beta

Using this configuration for my base router. Calling he controller base uri, e.g. /test will resolve TestController::indexAction() properly in phalcon 1.2.6

Since I updated to phalcon 1.3. ( windows dll ), it tries to resolve TestController::2Action as it is the action key in the routes config.

Although I use Router groups with a subcontroller mvc setup, this behaviour is the same for using router directly and include the prefix in each path definition

$router = new Phalcon\Mvc\Router(false);
$router->removeExtraSlashes(true);
$router->setDefaultAction( 'index' );
$router->setDefaultController( 'index' );
$router->add( '/', array(
    'controller' => 'index'
));
$hype = new \Phalcon\Mvc\Router\Group( array(
    'controller' => 'index',
    'action' => 'index'
) );
$hype->setPrefix( '/hype' );
$hype->add(
   # all 3 working in 1.2.6 and phalcon 1.3 alpha
   #"/(\w+)(?:/(\w+))?(/.*)*",
   #"/:controller(?:/:action)?(?:/:params)?",
   #"/([a-zA-Z0-9_-]+)(?:/([a-zA-Z0-9_]+))?(?:(/.*)*)?",

   "/([a-zA-Z0-9_-]+)(?:/([a-zA-Z0-9_]+))?(?:(/.*)*)?",

    array(
        "namespace" => 'Hype\Controller',
        "controller" => 1,
        "action" => 2,
        "params" => 3,
    )
);
$hype->add( '', array(
    "namespace" => 'Hype\Controller',
    'controller' => 'index',
    'action' => 'index'
));
$router->mount( $hype );

This simple test validates on phalcon 1.2.6 :

$testRoutes = array(
    '/',
    '/hype/',
    '/hype/',
    '/hype/controllerToken',
    '/hype/controllerToken/',
    '/hype/controllerToken/actionToken',
    '/hype/controllerToken/actionToken/',
);
/** @var \Phalcon\Mvc\Router $router */
$router = $di->get("router");
foreach ($testRoutes as $testRoute) {

    //Handle the route
    $router->handle($testRoute);

    echo 'Testing ', $testRoute, '<br>';

    //Check if some route was matched
    if ($router->wasMatched()) {
        echo 'Controller: ', $router->getControllerName(), '<br>';
        echo 'Action: ', $router->getActionName(), '<br>';
        echo 'Namespace: ', $router->getNamespaceName(), '<br>';
    } else {
        echo 'The route `'.$testRoute.'` wasn\'t matched by any route<br>';
    }
    echo '<br>';
}
// Correct behaviour in phalcon 1.2.6 :

Testing /
Controller: index
Action: index
Namespace: 

Testing /hype/
Controller: index
Action: index
Namespace: Hype\Controller

Testing /hype/
Controller: index
Action: index
Namespace: Hype\Controller

Testing /hype/controllerToken
Controller: controllerToken
Action: index
Namespace: Hype\Controller

Testing /hype/controllerToken/
Controller: controllerToken
Action: index
Namespace: Hype\Controller

Testing /hype/controllerToken/actionToken
Controller: controllerToken
Action: actionToken
Namespace: Hype\Controller

Testing /hype/controllerToken/actionToken/
Controller: controllerToken
Action: actionToken
Namespace: Hype\Controller

On phalcon 1.3 it doesn't resolve the action names. I get the numerical placeholders of the route config instead ( notice the Action: 2 output ) :



    // Incorrect  behaviour in phalcon 1.3 :

    Testing /
    Controller: index
    Action: index
    Namespace: 

    Testing /hype/
    Controller: index
    Action: index
    Namespace: Hype\Controller

    Testing /hype/
    Controller: index
    Action: index
    Namespace: Hype\Controller

    Testing /hype/controllerToken
    Controller: controllerToken
    Action: 2
    Namespace: Hype\Controller

    Testing /hype/controllerToken/
    Controller: controllerToken
    Action: 2
    Namespace: Hype\Controller

    Testing /hype/controllerToken/actionToken
    Controller: controllerToken
    Action: actionToken
    Namespace: Hype\Controller

    Testing /hype/controllerToken/actionToken/
    Controller: controllerToken
    Action: actionToken
    Namespace: Hype\Controller

What's going on here? Should I submit this as an issue on github?


98.8k

Could you try defined the routes this way:

$hype->add(
    "/:controller",
    array(
        "namespace" => 'Hype\Controller',
        "controller" => 1,
        "action" => 2
    )
);

$hype->add(
    "/:controller/:action/:params",
    array(
        "namespace" => 'Hype\Controller',
        "controller" => 1,
        "action" => 2,
        "params" => 3,
    )
);

$hype->add( '', array(
    "namespace" => 'Hype\Controller',
    'controller' => 'index',
    'action' => 'index'
));

$router->mount($hype);
edited Mar '14

Same issue. I also included routes test for the base routes that are handled by the router instance itself ( below the uri prefix for the router group )

$router = new Phalcon\Mvc\Router(false);
$router->removeExtraSlashes(true);
$router->setDefaultAction( 'index' );
$router->setDefaultController( 'index' );
$router->add( '(?:/(\w+))?(?:/(\w+))?(/.*)*', array(
    "controller" => 1,
    "action" => 2,
    "params" => 3,
));
$router->add( '/', array(
    'controller' => 'index',
));

$hype = new \Phalcon\Mvc\Router\Group( array(
    'controller' => 'index',
    'action' => 'index'
) );
$hype->setPrefix( '/hype' );

$hype->add(
    "/:controller",
    array(
        "namespace" => 'Hype\Controller',
        "controller" => 1,
        "action" => 2
    )
);

$hype->add(
    "/:controller/:action/:params",
    array(
        "namespace" => 'Hype\Controller',
        "controller" => 1,
        "action" => 2,
        "params" => 3,
    )
);

$hype->add( '', array(
    "namespace" => 'Hype\Controller',
    'controller' => 'index',
    'action' => 'index'
));
$router->mount( $hype );

Tested routes :

 $testRoutes = array(
  '/',
  '/test',
  '/test/routes',
  '/test',
  '/hype/',
  '/hype/controllerToken',
  '/hype/controllerToken/actionToken',

);

Output phalcon 1.3 ( windows dll ) :

Testing /
Controller: index
Action: index
Namespace: 

Testing /test
Controller: test
Action: 2
Namespace: 

Testing /test/routes
Controller: test
Action: routes
Namespace: 

Testing /test
Controller: test
Action: 2
Namespace: 

Testing /hype/
Controller: index
Action: index
Namespace: Hype\Controller

Testing /hype/controllerToken
Controller: controllerToken
Action: 2
Namespace: Hype\Controller

Testing /hype/controllerToken/actionToken
Controller: controllerToken
Action: actionToken
Namespace: Hype\Controller

Output phalcon 1.2.6 ( windows dll )

Testing /
Controller: index
Action: index
Namespace: 

Testing /test
Controller: test
Action: index
Namespace: 

Testing /test/routes
Controller: test
Action: routes
Namespace: 

Testing /test
Controller: test
Action: routes
Namespace: 

Testing /hype/
Controller: index
Action: index
Namespace: Hype\Controller

Testing /hype/controllerToken
Controller: controllerToken
Action: index
Namespace: Hype\Controller

Testing /hype/controllerToken/actionToken
Controller: controllerToken
Action: actionToken
Namespace: Hype\Controller

Note that in the above there is another issue for the route /test when /test/routes has been given to router->handle() before. In that case the action is still routes, but should be index as it was succesful in the initial test "Testing /test"

edited Mar '14

Ok, there was a typo in your code. Instead of

 $hype->add(
                "/:controller",
                array(
                    "namespace" => 'Hype\Controller',
                    "controller" => 1,
                    "action" =>2
                )
            );

it should be either

 $hype->add(
                "/:controller",
                array(
                    "namespace" => 'Hype\Controller',
                    "controller" => 1,
                    "action" =>'index'
                )
            );

or

 $hype->add(
                "/:controller",
                array(
                    "namespace" => 'Hype\Controller',
                    "controller" => 1,
                )
            );

simultanously for the base router

 $router->add( '/', array(
                'controller' => 'index',
            ));
            $router->add( '/:controller', array(
                "controller" => 1,
                "action" => 'index',
            ));
            $router->add(
                "/:controller/:action/:params",
                array(
                    "controller" => 1,
                    "action" => 2,
                    "params" => 3,
                )
            );

fixes all the issues there too,