Answer the question
In order to leave comments, you need to log in
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;
}
);
$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;
}
);
Answer the question
In order to leave comments, you need to log in
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',
]);
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question