如何使用Cake PHP创建子域? [英] How to create Sub Domains using Cake PHP?

查看:169
本文介绍了如何使用Cake PHP创建子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网站,每个设定档都需要一个专属的子网域。我使用的技术是Cake PHP + MongoDB。我需要知道同样的可行性。是否可以创建一个物理DNS或者我可以简单地路由用户的配置文件页面,在Cake PHP中的Routes类的帮助。

I am building a site ,which requires a unique sub-domain for each profile. The technology which I am using is Cake PHP + MongoDB. I need to know the feasibility of the same. Whether I can create a Physical DNS or I can simply route the Profile page of the User , by the help of Routes Class in Cake PHP. Please suggest what will be better and easy way to implement along with feasibility of the same.

感谢进阶。

推荐答案

如果您对每个网站使用CakePHP,则无需担心为每个网站创建多个子域,您只需创建通配符DNS记录即可。这会从服务器和DNS管理中消除很多麻烦。

If you are using CakePHP for each of the websites, you do not need to worry about multiple subdomain creation for each site, you can simply create a wildcard DNS record. This removes a lot of the hassle from the server and DNS administration.

通配符DNS记录(Wiki)

使用通配符DNS记录,您可以使用HTTP_HOST或CakePHP方式, $ request-> host()来确定用于访问系统的主机名(子域)。然后,您可以在AppController的beforeFilter()方法中加载正确的配置文件详细信息,然后从那里向前移动。

With the wildcard DNS record in place, you can then use HTTP_HOST, or the CakePHP way, $request->host() to determine the hostname (subdomain) being used to access your system. You can then load the correct profile details in the beforeFilter() method of AppController and move forward from there.

我会执行一些操作:

class User extends AppModel
{
     public $belongsTo = array(
          'Profile'
     );

     public function findByProfileHostname($hostname) {
         return $this->find('first', array('conditions' => array(
             'Profile.hostname' => $hostname
         ));
     }
};

class AppController extends Controller
{
    public function beforeFilter () {
        $this->ActiveUser = $this->User->findByProfileHostname($this->request->host());
        if (!$this->ActiveUser) {
            throw new InvalidArgumentsException(__('%s is not active here', $this->request->host()));
        }
    }
}

*免责声明:以上代码已从内存输入,可能无法通过复制/粘贴直接工作。

*disclaimer: Above code has been typed from memory and may not work directly through copy/paste.

希望这有助。

这篇关于如何使用Cake PHP创建子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆