Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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;
        }
    }


19.6k
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).



84.5k

You can change it in the router:

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

Or use the notFound paths option: http://docs.phalconphp.com/en/latest/reference/routing.html#not-found-paths



19.6k

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();


84.5k
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



19.6k

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