Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



40.3k
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. (: