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.

preg_match(): Compilation failed on $router->handle()

I get this error although the code works:

2015/02/20 21:49:21 [error] 3730#0: 35 FastCGI sent in stderr: "PHP message: PHP Warning: preg_match(): Compilation failed: unmatched parentheses at offset 9 in /home/stefan/websites/Bootstrap/bootstrap.php on line 130" while reading response header from upstream, client: 127.0.0.1, server: bootstrap.dev, request: "GET /sr/proba HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "bootstrap.dev"

This is the code, but let me first explain what I was trying to achieve:

  • I wanted to have translated routes and separate 404s for each module, so I have to find out language and module before defining router.

Line 130 would be $determinator->handle();

    /**
     * Registering language and module determinator
     */
    private function registerLanguageAndModuleDeterminator()
    {
        $config = $this->di->get('config');

        # Getting enabled languages
        $enabledLanguages = array();

        foreach ($config->languages as $language => $data)
        {
            if ($data['enabled'])
            {
                $enabledLanguages[] = $language;
            }
        }

        # Ghost router used to match modules and langs
        $determinator = new PhRouter(false);

        $determinator->setDefaults(array(
            'controller' => 'ghost',
            'action'     => 'ghost'
        ));

        # Is it frontend?
        $determinator->add('/:params)', array())->setName('frontend');
        # Is it backend?
        $determinator->add('/backend/:params', array())->setName('backend');

        $determinator->handle();

        $module = ($determinator->wasMatched()) ? $determinator->getRouteByName() : 'frontend';

        $this->di->set('isBackend', function() use ($module) {
            return ($module == 'backend');
        }, true);

        $this->di->set('module', function() use ($module) {
            return $module;
        }, true);

        $params = $determinator->getParams();

        $language = (isset($params[0]) && in_array($params[0], $enabledLanguages)) ? $params[0] : false;

        if ($language)
        {
            $this->di->set('lang', function() use ($language) {
                return $language;
            }, true);
        }
        else
        {
            $defaultLang = $config->application->defaultLang;
            $langMap     = $config->offsetGet('countryLang');

            # Get country based on GEOIP lookup
            $country = (isset($_SERVER['GEOIP_COUNTRY_CODE'])) ? strtolower($_SERVER['GEOIP_COUNTRY_CODE']) : false;

            $language = ($country && $langMap->get($country) && in_array($langMap->get($country), $enabledLanguages)) ? $langMap->get($country) : $defaultLang ;

            $this->di->set('lang', function() use ($language) {
                return $language;
            }, true);
        }

    }

It must be:

$determinator->add('/:params', array())->setName('frontend');

instead of:

$determinator->add('/:params)', array())->setName('frontend');


4.3k
Accepted
answer
edited Feb '15

Hey Andres, thanks I saw that myself when I posted, but now I get wrong number of parameters exception.

Wrong number of parameters
#0 /home/stefan/websites/Bootstrap/bootstrap.php(138): Phalcon\Mvc\Router->getRouteByName()
#1 /home/stefan/websites/Bootstrap/bootstrap.php(86): Application->registerLanguageAndModuleDeterminator()
#2 /home/stefan/websites/Bootstrap/bootstrap.php(45): Application->registerServices()
#3 /home/stefan/websites/Bootstrap/public/index.php(20): Application->_construct()
#4 {main}

EDIT: Scratch that, my bad. I misunderstood $route->getRouteByName() ....