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 cannot handle a dot in the URL

Hello. Phalcon seems to not be able to handle a dot in the URL when using the default routing behavior and when there is no controller specified in the URL. For example, when I try to load www.site.com/somecontroller/action.php, Phalcon throws a Not Found exception which is expected since there's no such controller, nor such action. However, when I enter www.site.com/action.php in the address bar, Phalcon erroneously loads the index action from the index controller instead of throwing a Not Found exception. Why is Phalcon behaving like this, and what can I do about it? I could adjust the htaccess file to not match dots in the URL, but that's not really a solution.

Here's my bootstrap file:

// Register the autoloader
$loader = new \Phalcon\Loader();

$loader->registerNamespaces([
    'ControlPanel' => '../ControlPanel/'
])->register();

// Create a dependancy injection
$di = new \Phalcon\DI\FactoryDefault();

// Registering a dispatcher
$di->set('dispatcher', function () {
    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setDefaultNamespace('ControlPanel\Controllers');
    return $dispatcher;
});

// Registering the view component
$di->set('view', function () {
    $view = new \Phalcon\Mvc\View\Simple();
    $view->setViewsDir('../ControlPanel/Views/');
    return $view;
});

$application = new \Phalcon\Mvc\Application($di);

// Since we are using Simple views, we must disable implicit views
$application->useImplicitView(FALSE);

echo $application->handle()->getContent();

What does your web server (nginx/apache) config look like for the virtual host?

You may be redirecting all requests to index.php



2.9k
edited Mar '18

Thanks for the reply! That's a good guess! I'm not very tech-savvy, so I'm not sure what part of the configuration you want me to post, but I believe there is no redirection to /index.php. I can see from phpinfo() that htaccess properly rewrites /action.php to /index.php?_uri=/action.php. Then Phalcon, for some reason, doesn't throw an exception that there is no such method in the Index controller. I'm using a Linux VPS, but I decided to test the same scenario via XAMPP on my Windows machine, and the same thing is happened.

Phalcon's default routing behavior (/controller/action/params, or /action/params when trying to load an action from the index controller) doesn't seem to support file extensions in the URL. While that's sort of understandable, what should happen is /action.php should throw an exception instead of rendering the index action from the index controller as if I've tried to access /, /index or /index/index. My guess is that this is a small overlooked detail in the default routing code. If this is the case, fixing it would be great.

My main issue stems from the fact that all modern browsers try to fetch /favicon.ico. If there is no actual favicon.ico file in the public dir however, Phalcon returns the /index/index action. Thus, if you visit any page, effectively your browser loads 2 pages - the actual page that you're requesting, and the /index/index page that Phalcon returns when the browser requests the /favicon.ico file. This is not desirable and, as you can imagine, is potentially dangerous.

If there is no other solution, I think I'm going to cope with this through htaccess, or in other words I'll make so that all pages containing a dot don't get rewriten to index.php, and consequently handled by Phalcon. This, however, is only a temporary solution, and I'd be so happy if the Phalcon team fixes the cause of the problem.