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

Case insesitive routing, how to?

How can I have case insesitive URL?

https://localhost:8188/login - OK

https://localhost:8188/logiN - Error 404, but I want to do loginAction ....

Thanks for help

Router settings

    $di->set ( 'router', function () {
        $router = new \Phalcon\Mvc\Router ( false );

        $router->add ( "/login", array (
                "controller" => "index",
                "action" => "login"
        ) );

        $router->add ( "/(.*)-{productid:[0-9]+}", array (
                "controller" => "products",
                "action" => "index" 
        ) );
        $router->notFound ( array (
                "controller" => "index",
                "action" => "route404" 
        ) );
        $router->setDefaults ( array (
                "controller" => "index",
                "action" => "route66" 
        ) );
        return $router;
    } );
edited Mar '14

You can setup your dispatcher to lowercase the route requests like this:

$di->set('dispatcher', function () {
    $events_manager = new \Phalcon\Events\Manager;

    $events_manager->attach('dispatch:beforeDispatchLoop', function ($event, $dispatcher) {
        $dispatcher->setControllerName(\Phalcon\Text::lower($dispatcher->getControllerName()));

        if ($dispatcher->getActionName()) {
            $dispatcher->setActionName(\Phalcon\Text::lower($dispatcher->getActionName()));
        }

    });

    $dispatcher = new \Phalcon\Mvc\Dispatcher;

    $dispatcher->setEventsManager($events_manager);

    return $dispatcher;
});

I haven't verified that code 100% but it should work.



3.0k

Doesn't work :(



3.0k
Accepted
answer
edited Mar '14

My solution (after read a lot of Apache doc)

httpd.conf - add RewriteMap and enable proxy.

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
...

<VirtualHost *:8188>
DocumentRoot "C:/Eclipse 4 Workspace/phalcon/html/www/"
php_admin_flag engine On
RewriteMap lowercase int:tolower
    ProxyRequests On
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php?_url=/${lowercase:$1} [QSA,L,P]
</IfModule>


98.9k

You can add at the top of your index.php:

if (isset($_GET['_url'])) {
     $_GET['_url'] = strtolower($_GET['_url']);
}


3.0k
edited Mar '14

It is working too, thank You.

Sorry about that. Glad to hear you got it working though, both by .htaccess and Phalcon's suggestion. :)