404 and notFoundAction

Hi.

My notFoundAction() in ControllerBase never seems to be called.

I can do something like this; http://stackoverflow.com/questions/14071261/how-to-setup-a-404-page-in-phalcon but I thought notFoundAction() was "out-of-the-box". Didn't think I had to use ->forward(...) either.

What am I doing wrong? Thanks in advance.

(Regards from sunny Queensland, Australia)



4.8k

Does the IndexController extends from your ControllerBase ?

Or you can set it in your router

$router = new Phalcon\Mvc\Router() $router->notFound(array( "controller" => "action" => whatever ))

Thanks for the reply - yes it does extend ControllerBase ?

I tried that snippet too - but no joy. If need be I'll post code; mainly just wondering why notFoundAction() is never called ("out-of-the-box" ) Still have

PhalconException: Action 'foo' was not found on handler 'index'



84.1k

There is no such "notFoundAction" (out-of-the-box), implement that method on a controller could be insufficient, a not-found event is triggered when:

1) The passed URI to Phalcon\Mvc\Router does not match any of the routes set up on it 2) The router matches a URI but Phalcon\Mvc\Application can't load the module (if any) 3) The router matches a URI but Phalcon\Dispatcher can't find the controller file 4) The router matches a URI but Phalcon\Dispatcher can't find the controller class 5) The router matches a URI but Phalcon\Dispatcher can't find the action method in the controller class

Implement an action "notFound" would only work in the case 5.



4.8k

Thanks for the explanation, so where is the correct place to register not-found event handler ? Dispatcher ?



84.1k

Yes, most of the time intercepting these events in the dispatcher will allow to show a 404 to your users:

use Phalcon\Events\Manager as EventsManager,
    Phalcon\Dispatcher as Dispatcher;

$di['dispatcher'] = function() {

    $evManager = new EventsManager();

    $evManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {
        switch ($exception->getCode()) {
            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                $dispatcher->forward(array(
                    'controller' => 'error',
                    'action'     => 'show404',
                ));
                return false;
            }
    });

    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($evManager);
    return $dispatcher;
};

Hi,

I implemented 404 errors as specified above, but the problem I am facing with is that the response is empty. Only the headers are sent. Can anyone help me?

Thanks



3.7k

How to make module not registered show 404 page?