We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon route groups 404 not working

I have URL's:

site.com/404 - 404 Working

site.com/ru/404 - 404 not working, I get redirect to main page

My routes:

$router = new \Phalcon\Mvc\Router(false);

$router->notFound(array(
  "controller" => "Notfound",
  "action" => "index"
));

$router->add('/', array(
  'module' => 'frontend',
  'controller' => 'index',
  'action' => 'index',
  'lang' => 'md',
))->setName('default');

$router->add('/{lang:[' . $langsDefined . ']{2}+}(/?)', array(
  'module' => 'frontend',
  'controller' => 'index',
  'action' => 'index',
  'lang' => 1,
))->setName('default_lang');

$router->add('/{controller:[a-z]{3,50}+}(/?)', array(
    'module' => 'frontend',
    'controller' => 1,
    'action' => 'index',
    'lang' => 'md',
))->setName('default_module');

$router->add('/{lang:[' . $langsDefined . ']{2}+}/{controller:[a-z]{3,50}+}(/?)', array(
    'module' => 'frontend',
    'controller' => 2,
    'action' => 'index',
    'lang' => 1,
))->setName('default_module_lang');

$router->add('/{controller:[a-z]{3,50}+}/:action/', array(
    'module' => 'frontend',
    'controller' => 1,
    'action' => 2,
    'lang' => 'md',
))->setName('default_module_controller_action');

$router->add('/{lang:[' . $langsDefined . ']{2}+}/{controller:[a-z]{3,50}+}/:action/', array(
    'module' => 'frontend',
    'controller' => 2,
    'action' => 3,
    'lang' => 1,
))->setName('default_module_controller_action_lang');

$router->add('/{controller:[a-z]{3,50}+}/:action/:params/', array(
    'module' => 'frontend',
    'controller' => 1,
    'action' => 2,
    'params' => 3,
    'lang' => 'md',
))->setName('default_module_controller_action_params');

$router->add('/{lang:[' . $langsDefined . ']{2}+}/{controller:[a-z]{3,50}+}/:action/:params/', array(
    'module' => 'frontend',
    'controller' => 2,
    'action' => 3,
    'params' => 4,
    'lang' => 1,
))->setName('default_module_controller_action_params_lang');

How I can fix this:?

My function run in Bootstrap:

public function run() {
        try{

            $this->general_configs = require_once APPLICATION_PATH . DS . 'configs' . DS . 'application.php';
            $this->redis_configs = require_once APPLICATION_PATH . DS . 'configs' . DS . 'redis_configs.php';
            $this->redis_cluster_configs = require_once APPLICATION_PATH . DS . 'configs' . DS . 'redis_cluster_configs.php';

            $this->initAll();

            $first_page_url = array('/', '/ru/','/md/');

            //main page from cache or refrash
            //$auth = $this->di->get('session')->get('auth', false);

            $use_home_page_cache = false; // $this->general_configs['use_home_page_cache'];
            //if($use_home_page_cache && in_array($_SERVER['REQUEST_URI'], $first_page_url) && !$auth) {
            if($use_home_page_cache && in_array($_SERVER['REQUEST_URI'], $first_page_url)) {
                $cache = $this->di->get('cache5min');
                $key = 'home_page_cache_'.$_SERVER['REQUEST_URI'];
                if(isset($_GET['cache_renew_now']) && $_GET['cache_renew_now'] == 1) {
                    $cache->delete($key);
                }
                if(($out = $cache->get($key)) === NULL  || $this->di->get('cache_renew_data')->can_refrash) {
                    $out = $this->application->handle()->getContent();
                    $out = compress_page($out);
                    $cache->save($key, $out);
                }
                // send content
                echo $out;
            } else {
                // send content if not home page
                $out = $this->application->handle()->getContent();
                $out = compress_page($out);
                echo $out;
            }

            if(isset($_COOKIE['deb'])) {
                echo '<br style="clear: both;" /><pre>' . print_r($this->di->get('cache_renew_data')->getData(), true).'</pre>';
                //var_dump($this->di->get('profiler'));
            }

            //$this->application->response->set
            //$this->application->handle()->send();

        } catch (\Phalcon\Exception $phE) {
            if(APPLICATION_ENV != 'production') {echo '\Phalcon\Exception<pre>'.print_r($phE, true).'</pre>';}
            else { header("HTTP/1.1 404 Not Found");
                $this->notFound();
            }
        } catch (PDOException $pdoE) {
            if(APPLICATION_ENV != 'production') {echo 'PDOException<pre>'.print_r($pdoE, true).'</pre>';}
            else { echo 'DB Error'; exit; }
        } catch (Exception $e) { die($e->getMessage());
            if(APPLICATION_ENV != 'production') {echo 'Exception<pre>'.print_r($e, true).'</pre>';}
            else { $this->notFound();}
        }

        //if(APPLICATION_ENV != 'production') { echo '<pre>'; var_dump($this->di->get('profiler')); echo '</pre>';};
    }

Place this rule

$router->notFound(array(
  "controller" => "Notfound",
  "action" => "index"
));

at the end of the chain, afaik Phalcon traverses the routes array in reverse



1.5k

:(

I put on end, but not working, I get redirect on main page. When: site.com/ru/404


$router->notFound(array(
    "controller" => "Notfound",
    "action" => "index"
));

return $router;

Place this rule

$router->notFound(array(
 "controller" => "Notfound",
 "action" => "index"
));

at the end of the chain, afaik Phalcon traverses the routes array in reverse