Category: Routing

Q: Cakephp 3 create sub-domain routing like username.domain.com

I want to create sub-domain routing for my Cakephp 3 application. The user profile url is:

http://domain.com/profile/{username}

I would like to have sub-domain {username} pointing to ProfileController, for example:

http://{username}.domain.com  =>  /profile/{username}

I have no idea how to achieve this.

subdomain
1 Answers

Version: 3.0

I'm not sure if this helps CakePhp and subdomain routing.

<?php

class SubdomainRoute extends CakeRoute {

    public function match ($params) {
        $subdomain = isset($params['subdomain']) ? $params['subdomain'] : null;
        unset($params['subdomain']);
        $path = parent::match($params);
        if ($subdomain) {
            $path = 'http://' . $subdomain . '.localhost' . $path;
        }
        return $path;
    }
}