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 limit all browsing just to my routes?

is it possible that all requests except my defined routes are routed to a certain controller/action?

edited Nov '14

Yes of course, you can handle it with event manager easily. just add your specific routes to your router the add this code to your application's bootstrap file

$dispatcher = new \Phalcon\Mvc\Dispatcher();
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) use ($di)
{
    if ($event->getType() == 'beforeNotFoundAction') {
        $dispatcher->forward(array(
            'module'     => 'YOUR MODULE',
            'controller' => 'YOUR CONTROLLER',
            'action'     => 'YOUR ACTION'
        ));

        return false;
    }
});
$dispatcher->setEventsManager($eventsManager);

$di->setShared('dispatcher', $dispatcher);

It will forward all undefined route to YOUR_MODULE/YOUR_CONTROLLER/YOUR_ACTION

You can ignore MODULE if you have a single module app

Correct me if I'm wrong, but Aboozar's solution will only work if the controller is correct but the action isn't found. If the dispatcher fails to find a controller, it throws a 'beforeException' event instead. So you'll need to check for that as well if you need it.

Use default controller/action for dispatcher?

$dispatcher = \Phalcon\DI::getDefault();
$dispatcher->setDefaultController( ... your default controller ... );
$dispatcher->setDefaultAction( ... your default action ... );


10.7k

I'm not sure if prevoius response works.. (I just do not know)

But you also can modify this plugin for your needs : https://github.com/phalcon/invo/blob/master/app/plugins/NotFoundPlugin.php

Setting default Controller and Action the Way I presented works, see for yourself, it's easy to test it.