1

how to create 404 error page for manual bootstrap for example in this app ? http://album-o-rama.phalconphp.com/

i use this dispatcher :

$di->set(
'dispatcher',
function() use ($di) {

    $evManager = $di->getShared('eventsManager');

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

);

Phantom
  • 1,704
  • 4
  • 17
  • 32
a.4j4vv1
  • 123
  • 3
  • 11

4 Answers4

4

Try this in your index.php:

$di->set('dispatcher', function() {

    $eventsManager = new \Phalcon\Events\Manager();

    $eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {

        //Handle 404 exceptions
        if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
            $dispatcher->forward(array(
                'controller' => 'index',
                'action' => 'show404'
            ));
            return false;
        }

        //Handle other exceptions
        $dispatcher->forward(array(
            'controller' => 'index',
            'action' => 'show503'
        ));

        return false;
    });

    $dispatcher = new \Phalcon\Mvc\Dispatcher();

    //Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;

}, true);
Phantom
  • 1,704
  • 4
  • 17
  • 32
  • when use this dispatcher also show this error: Service 'view' was not found in the dependency injection container – a.4j4vv1 Jun 28 '14 at 18:12
1

The recommended functionality here is:

http://docs.phalconphp.com/en/latest/reference/routing.html#not-found-paths

and maybe

routing.html#dealing-with-extra-trailing-slashes

For manual bootstrap, instead of use dispatcher, you could set a router

/**
 * Registering a router
 */
$di->set('router', require __DIR__.'/../common/config/routes.php');

Then add this route rule at 'common/config/routes.php'.

$router->notFound(array(
    'module' => 'frontend',
    'namespace' => 'AlbumOrama\Frontend\Controllers\\',
    'controller' => 'index',
    'action' => 'route404'
));

Finally define a controller and a view to capture this action.

And voilà: 404 error page!

Just for comment, I pull request this solution for the app you mentioned:

https://github.com/phalcon/album-o-rama/pull/5/files

mfer
  • 11
  • 2
1

For new version of Phalcon you can handle error using routes by adding this code to service.php

$di->set('router',function() use($Config){
    $router = new \Phalcon\Mvc\Router();
    $router->notFound(array(
        "controller" => "error",
        "action" => "error404"
    ));
    return $router;
}); 
Kamran
  • 2,711
  • 2
  • 17
  • 24
-2
public function show404Action()
{
    $this->response->setStatusCode(404, 'Not Found');
    $this->view->pick('error/show404');
}
a.4j4vv1
  • 123
  • 3
  • 11
  • 1
    Answers work much better with text explaining them. Explanations facilitate learning. This answer actually showed up in the review queue for low quality posts because of this. – cfi Jun 28 '14 at 20:35