A
A
Arseny Sokolov2017-04-27 06:34:37
Phalcon
Arseny Sokolov, 2017-04-27 06:34:37

How to handle routes with numbers in Phalcon?

Such a situation, if we try to open a page at this address:
localhost/404
Then we get to the start page and so on with any numbers, i.e. addresses that start with numbers always lead to the start page, although there are no controllers, no corresponding methods, processing of non-existent controllers or actions sends to a 404 page, but digital requests never send to a 404 page.
This problem can somehow be solved, who knows?
Here is the routing:

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

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

Here is the error handler in the default 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;
            }
        );

By the way, for clarity, what the command shows in the index view:
Y2L4XL5c6ZxD26.pngJ2by1EqHPpMXm6.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arseniy Sokolov, 2017-04-28
@ArsenBespalov

Maybe it's just me who has such problems with the numbers in the address, but she really got me, as a result, she made the solution so uncomplicated:

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

Thanks 1 1 for helping me solve my problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question