We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

How to set 404 page in http://localhost/404?

I don't understand how to set 404 page for any start number route and don't route to default index page.

If I goto http://localhost/404 (or any numbers), I see default index page, not 404 page.

This route:

    $di->set('router', function () {
        $router = new Router(false);

        $router->setDefaultModule('frontend');

        $router->add('/:action', [
            'module' => 'frontend',
            'controller' => 'index',
            'action' => 1,
        ]);

        $router->add('/:action/:params', [
            'module' => 'frontend',
            'controller' => 'index',
            'action' => 1,
            'params' => 2,
        ]);

        $router->add('/:module/:controller/:action/:params', [
            'module'     => 1,
            'controller' => 2,
            'action'     => 3,
            'params'     => 4,
        ]);

        $router->removeExtraSlashes(true);

        return $router;
    });

And this dispatcher exception for default frantend module:

        $dependencyInjector->setShared(
            'dispatcher',
            function () {
                $eventManager = new Manager();

                $eventManager->attach(
                    'dispatch:beforeException',
                    function ($event, $dispatcher, $exception) {
                        switch ($exception->getCode()) {
                            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                                $dispatcher->forward([
                                    'controller' => 'error',
                                    'action' => 'notFound',
                                ]);
                                return false;
                        }
                    }
                );

                $dispatcher = new Dispatcher();

                $dispatcher->setDefaultNamespace('AudioOcean\Frontend\Controllers');
                $dispatcher->setEventsManager($eventManager);

                return $dispatcher;
            }
        );

did you attempt to use the notfound command in the routes?

// Set 404 paths
$router->notFound(
    [
        "controller" => "index",
        "action"     => "notfound",
    ]
);

I used this method, but nothing has changed. I use Nginx as http proxy server, and use php-fpm. Phalcon version 3.2.0 alpha 1.

did you attempt to use the notfound command in the routes?

// Set 404 paths
$router->notFound(
   [
       "controller" => "index",
       "action"     => "notfound",
   ]
);
edited Apr '17

If show in index view:

<?php echo 'Action Name: ', $this->router->getActionName(), '<br><br>'; ?>

if I use this code in IndexController:

public function initialize()
    {
        if (!$this->router->getActionName()) {
            $this->dispatcher->forward([
                'controller' => 'error',
                'action' => 'notFound',
            ]);
        }
    }

I see this error:



1.4k
Accepted
answer
edited Apr '17

I resolve my problem, just use this code:

$eventManager->attach('dispatch', function (Event $event, Dispatcher $dispatcher) {
                    if (!$dispatcher->getActionName()) {
                        $dispatcher->forward([
                            'controller' => 'error',
                            'action' => 'notFound',
                        ]);
                    }
                });

Thanks all for help me.