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

Basic routing usage

So, basically just started playing with routes, and I am already having trouble.

Basically, what I'm trying to achieve is something like this:

// When user type an URL like this
.../my-phalcon-project/activate/12345

//the '12345' should be a parameter retrieved in indexAction from activateController

pretty simple, right? But I can't get this working.

This is the code used in router:

#...
$router->add(
    "/activate/{hash}",
    array(
        "controller" => "activate",
        "action"     => "index",
    )
);

And in the ActivateController:

   # indexAction()...
    print_r($this->dispatcher->getParam("hash"));

Aaand, nothing is showed. Any idea of what I'm doing wrong here?

Hi @Cloudio,

I tried your code and everything worked as exepcted. When I type example.com/activate/12345 it prints out 12345. The only thing I added to my router file was:

//Remove trailing slashes automatically
$router->removeExtraSlashes(true);

If the browser adds automatically a slash at the end, you will get a blank page. RemoveExtraSlashes solved this problem.



58.8k
Accepted
answer
edited May '14

@screenas,

You were right, the code itself was correct. The problem was that I was calling my routes.php file direct into public/index.php instead of set it at services.php using FactoryDefault().

All I made was remove it from index.php and then add this to my services.php file:

$di->set('router', function () {
    return require __DIR__ . '/routes.php';
});

Anyway, thanks for letting me know that the code wasn't the problem. (: