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

subdomain routing and link generation

Hi there, do you guys have an elegant solution for routing subdomain to the module / controller? Currently I have a virtual host that handle wildcard subdomain into one phalcon multi module project. A subdomain can route to specific module (e.g. m.domain.com routes to mobile module) or to a specific controller. My current solution is prepending the $_GET['_url'] with the subdomain and add a route for that. For example _url for https://m.domain.com/xxx/yyy is rewrited to /m/xxx/yyy and add a routing rule for /m/:controller/:action. But I think it would cause a duplicate content and I have to implement my own Phalcon\Mvc\Url and Tag::linkTo to generate a subdomain link. Any enlightment here please :)



98.9k

Routing based on the hostname is still not supported by Phalcon, maybe you can implement this by adding routes conditionally based on the current host:

<?php

$di['router'] = function() {

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

    switch ($_SERVER['HTTP_HOST']) {
        case 'm.domain.com':

            $router->add('/m/xxx/yyy', array(
                'controller' => 'xxx',
                'action' => 'yyy'
            ));

            //...
            break;
        default:
            $router->add('/xxx/yyy', array(
                'controller' => 'xxx',
                'action' => 'yyy'
            )); 
            break;
    }

    return $router; 
};


32.5k

Now I thinking about domain-routing too. It may be very useful to divide service components from user components, for example uri which payments systems send success-payment-request to. I hope this feature will be supported by Phalcon directly and comfortable in future.

Any way you could implement this feature in 1.0.0 before it gets out of beta? I'll need this and switch is no solution for a SaaS ;)

HTTP_HOST is populated from "Host:xxxxx " header coming with request. It makes domain routing not safe, just google "HTTP_HOST spoofing".

I had this idea before but reconsidered and now I just use another index script file and virtual domain setup. Routes I keep in separate files, but bootstrap file is common for each application.

I just try to think if htacess could solve this issue but again it will mean rewriting every url used with the subdomain