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

Custom routes not working

I'm trying to create routes like this:

/accounts
/accounts/create
/accounts/6
/accounts/6/edit
/accounts/6/transactions
/accounts/6/transactions/creat
.
.
.

I'm writing the following to a file /app/config/routes.php and including it in my /public/index.php file but I doesn't seem to be right. Is there anything I'm doing wrong?

<?php

// Creating a router
$router = new \Phalcon\Mvc\Router();

$router->addGet("/", "Accounts::index");

$router->addGet("/accounts", "Accounts::index");
$router->add("/accounts/create", "Accounts::create");
$router->add("/accounts/{id}/edit", "Accounts::edit");
$router->add("/accounts/{id}/delete", "Accounts::delete");

$router->addGet("/accounts/{account_id}/transactions", "Transactions::index");
$router->add("/accounts/{account_id}/transactions/create", "Transactions::create");
$router->add("/accounts/{account_id}/transactions/{id}/edit", "Transactions::edit");
$router->add("/accounts/{account_id}/transactions/{id}/delete", "Transactions::delete");


98.9k

Try disabling default routes:

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

https://docs.phalcon.io/en/latest/reference/routing.html#setting-default-paths

I tried that but it's still giving me the same result.

To debug, I'm going into my SecurityPlugin and var_dump 'ing the action name:

class SecurityPlugin extends plugin
{

    ...

    public function beforeDispatch(Event $event, Dispatcher $dispatcher)
    {
        ....

        $controller = $dispatcher->getControllerName();
        $action = $dispatcher->getActionName();

        ....

        var_dump($action); exit;

        ....
    }
}

...but when I access the URL - /accounts/6 - which according to my routes should map to accounts controller's show action. But, it's giving me "6" as the action which would be the default route (/:controller/:action). As you can see from my routes, this hasn't been defined (second URL component as action that is).

By the way, do I not need to inject this $route object into the app somehow? I'm unsure, how the app uses it. I fel it should be in config or something but I can't see where it instructs me to do so (if that's where it belongs).

Thanks