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;
    }
}

Created: 28 Sep '16

Last Reply: 4 Oct '16

Replies: 1

Views: 1017

Votes: 0

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions