如何使用Cake PHP创建子域?

编程入门 行业动态 更新时间:2024-10-18 14:20:38
本文介绍了如何使用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创建子域?

本文发布于:2023-10-31 16:06:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Cake   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!