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

Problem with Router that I don't understand

Hi there!

I have a problem with the Router... It always executes the "not found" middleware.

Here are the routes :

array (size=1)
  0 => 
    object(Phalcon\Mvc\Router\Route)[99]
      protected '_pattern' => string '/{language:(en|fr)}/' (length=20)
      protected '_compiledPattern' => string '#^/(en|fr)/$#' (length=13)
      protected '_paths' => 
        array (size=1)
          'language' => int 1
      protected '_methods' => string 'GET' (length=3)
      protected '_hostname' => null
      protected '_converters' => null
      protected '_id' => int 2
      protected '_name' => null
      protected '_beforeMatch' => null
      protected '_group' => null

Here is the URL I am trying to access:

https://www.myapp.local/fr/

Is there something wrong in my configuration?

Here is the $_SERVER variable:

array (size=39)
  'REDIRECT_STATUS' => string '200' (length=3)
  'HTTP_HOST' => string 'www.myapp.local' (length=14)
  'HTTP_CONNECTION' => string 'keep-alive' (length=10)
  'HTTP_CACHE_CONTROL' => string 'max-age=0' (length=9)
  'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' (length=74)
  'HTTP_USER_AGENT' => string 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36' (length=109)
  'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate, sdch' (length=19)
  'HTTP_ACCEPT_LANGUAGE' => string 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4' (length=35)
  'SystemRoot' => string 'C:\Windows' (length=10)
  'COMSPEC' => string 'C:\Windows\system32\cmd.exe' (length=27)
  'PATHEXT' => string '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC' (length=53)
  'WINDIR' => string 'C:\Windows' (length=10)
  'SERVER_SIGNATURE' => string '<address>Apache/2.4.9 (Win64) PHP/5.5.12 Server at www.myapp.local Port 80</address>
' (length=84)
  'SERVER_SOFTWARE' => string 'Apache/2.4.9 (Win64) PHP/5.5.12' (length=31)
  'SERVER_NAME' => string 'www.myapp.local' (length=14)
  'SERVER_ADDR' => string '127.0.0.1' (length=9)
  'SERVER_PORT' => string '80' (length=2)
  'REMOTE_ADDR' => string '127.0.0.1' (length=9)
  'DOCUMENT_ROOT' => string 'D:/wamp/www/myapp/public/portal' (length=30)
  'REQUEST_SCHEME' => string 'http' (length=4)
  'CONTEXT_PREFIX' => string '' (length=0)
  'CONTEXT_DOCUMENT_ROOT' => string 'D:/wamp/www/myapp/public/portal' (length=30)
  'SERVER_ADMIN' => string '[email protected]' (length=17)
  'SCRIPT_FILENAME' => string 'D:/wamp/www/myapp/public/portal/portal.php' (length=41)
  'REMOTE_PORT' => string '50488' (length=5)
  'REDIRECT_QUERY_STRING' => string '_url=/fr/' (length=9)
  'REDIRECT_URL' => string '/fr/' (length=4)
  'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
  'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
  'REQUEST_METHOD' => string 'GET' (length=3)
  'QUERY_STRING' => string '_url=/fr/' (length=9)
  'REQUEST_URI' => string '/fr/' (length=4)
  'SCRIPT_NAME' => string '/portal.php' (length=11)
  'PHP_SELF' => string '/portal.php' (length=11)
  'REQUEST_TIME_FLOAT' => float 1425833241.949
  'REQUEST_TIME' => int 1425833241

Thank you all!

Best regards,

please give your router initialization code. (your app bootstrap code)

did you have MVC app or Micro app?

Hi Aboozar,

I am using \Phalcon\Mvc\Micro

I already have 2 other apps like this one, they don't fail. Plus another app at the office, which works too.

That is why I don't understand where I did a mistake...

Here is my app bootstrap :

$app = new \MyApp\App($di); // extends \Phalcon\Mvc\Micro
$di->setShared('app', $app);

$loader = new \Phalcon\Loader();
$aNamespaces = array();
$aModules = $di->get('config')->application->portal->modules;
foreach ($aModules as $sModule)
{
    $sNamespace = ucfirst($sModule);
    $aNamespaces['MyApp\Controller\\' . $sNamespace] = PATH_APP . 'modules/' . $sModule . '/controller';
}
$loader->registerNamespaces($aNamespaces);
$loader->register();
$app->notFound(function() use ($app)
    {
        die('Not found');
    });

function _getNewController($sModule, $sName, $sPrefix = null)
    {
        $oColl = new Phalcon\Mvc\Micro\Collection();
        $sHandler = 'MyApp\Controller\\' . $sModule . '\\' . $sName . 'Controller';
        $oColl->setHandler($sHandler, true);
        $oColl->setPrefix($sPrefix ?: '/' . strtolower($sName));
        return $oColl;
    }

foreach ($aModules as $sModule)
    {
        require_once(PATH_APP . 'modules/' . $sModule . '/config/routing.php');
    }

$app->handle();

Routing.php :

$oColl = _getNewController('Main', 'Main', '/');

$oColl->get('{language:(en|fr)}/', 'home');

$app->mount($oColl);

Do you see anything weird in my code, by any chance?

Okay... I found the issue. It comes from the trailing slash in my route definition. If I remove it, it works fine. However, it also works for "/fr" (without trailing slash) which is not what I want.

Is this a bug that I should report somewhere?