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

How to restrict accessing an action of a controller directly

I want to make the accessing to the contoller PostController::showAction, only from this url: https://localhost/blog/post/:id/:title and not from https://localhost/blog/post/show

So I did this to my router:

$di->set("router", function(){
        $router = new \Phalcon\Mvc\Router();

        $router->add(
            "/post/([0-9]+)/([a-zA-Z0-9\+\_\-]+)",
            array(
                "controller" => "post",
                "action"     => "show",
                "id"         => 1,
                "title"      => 2
            )
        );
        return $router;
    });

But it still can be accessed from the unwanted url (https://localhost/blog/post/show) I think that the router should do this restriction, how ?



98.9k
Accepted
answer

Pass false as first parameter of Phalcon\Mvc\Router constructor to disable the default routes:

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