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

Redirecting 'contact-us' to ContactController is not working

I am using INVO example app from here:

https://github.com/phalcon/invo

I have copied all the files and set the db and base url.

It works, I can login etc.

however, I wanted to learn how to use redirects e.g.

I would like to use contact-us instead of contact without cahnging the name of the controller.

So, I created a file routes.php inside of the app/config folder and put this code inside routes.php :

<?php

$router->add('/contact-us', array(
    'controller' => "contact",
    'action' => "index"
))->setName('contact');

However, it's not working and when I try to access https://localhost/test/contact-us I am getting this error:

You don't have access to this module

The original url https://localhost/test/contact works ok.

What am I doing wrong?

Do I need to set something else in Phalcon? Btw. how is the routes.php file loaded? Is it done automatically or do I need to set it somewhere? If so how?



37.0k
Accepted
answer
edited Jul '14

I have kind of worked it out by changing the code to this:

<?php
$router = new Phalcon\Mvc\Router(true);

$router->add('/contact-us', array(
    'controller' => "contact",
    'action' => "index"
));

return $router;

Btw. notice that if you change Phalcon\Mvc\Router(true); to Phalcon\Mvc\Router(false); default routes will stop working, meaning the original default route:

https://localhost/invo/contact

will stop working (you won't be redirected to the ContactController) and you will end up on the INVO homepage.

And only the new route:

https://localhost/invo/contact-us

will be redirecting to the ContactController