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

About index/index

Which event in the dispatcher is the one that, when the URL is www.domain.es (you know, searching for default index/index), executes? I've tried beforeExecuteRoute, beforeDispatch and beforeNotFoundAction (because I don't have an index action), but I always get the exception for not finding the action.

I have the following event:

public function beforeException(Event $event, Dispatcher $dispatcher, $exception)
    {
        switch ($exception->getCode()) {
            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                $dispatcher->forward(
                    array(
                        'controller' => 'error',
                        'action'     => 'notfound',
                    )
                );
                return false;
        }
    }


33.8k
edited Nov '14

I tought the same, 'cause it throws an exception. It is a solution, but I think this will be a case that fits better in one of the other events I used. BTW, I don't wanna redirect to an error controller, just investigate about my problem.

The problem is that beforeDispatch (who is the very first event triggered in that context, I think), knows that I'm calling index/index, but it just doesn't do the redirection in the IF I use (if the controller and action names are index, do a redirection to index/anotherAction).



98.9k

You can change it in the router:

$router->setDefaultController('other');
$router->setDefaultAction('myaction');

Or use the notFound paths option: https://docs.phalcon.io/en/latest/reference/routing.html#not-found-paths



33.8k

I cannot get it to work. Even with your code, and with what I've found in the Router docs:

    $router = new \Phalcon\Mvc\Router();
    $router->add('/', 'Index::conexion');
    $router->setDefaultController('index');
    $router->setDefaultAction('conexion');
    $router->notFound([
        'controller'    => 'index',
        'action'        => 'conexion'
    ]);
    $router->handle();


98.9k
Accepted
answer

$router->handle(); must not be called manually, just return the $router in a service definition https://github.com/phalcon/php-site/blob/master/public/index.php#L36



33.8k

Aww, a service, why I didn't think that it was a service? Now it makes sense, I forgotted it. Thanks.