CakePHP:重用模型、控制器、配置,但不重用视图和路由
我们的网站将按如下方式运行:
- 有一个中心网站 www.example.org,每个人都可以在其中注册、更改信息、管理他们的“名片”。
- 此外,每个人都会有 companyname.example.org。在这里,用户可以根据他们在主站点中更改的信息发布一个简单的站点。
该网站是基于 CakePHP 构建的。我想知道这有什么好的设置。在进行一些谷歌搜索后,为控制器和模型创建一个单独的目录以供重用似乎很常见,但在这种情况下,我想真正共享除路由和视图之外的所有代码。
是否可以根据域名更改路由。这是否被认为“适合”CakePHP?我更愿意找到最佳实践,而不是“完成”工作的解决方案。
谢谢!
Our website is going to work as follows:
- There's a central site www.example.org where everybody can register, change information, manage their 'business card'.
- Also, everybody will have companyname.example.org. Here users can publish a simple site based on information they change in the main site.
This site is being built on CakePHP. I'm wondering what a good setup for this is. After doing some googling it appears to be common to create a separate directory for controllers and models for reusing, but in this case I want to really share all the code, except routes and views.
Is it possible to change routes based on a domainname. Would this be considered 'appropriate' for CakePHP at all. Rather than a solution that 'does' the job, I would prefer to find the best practice.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用本教程解决
Solved using this tutorial
在我的脑海中,CakePHP 中的路由引擎没有域的概念,因此理论上您可以做的就是使用 Apache rewrite
在路线中添加前缀是可能的,因为我之前用它来生成多语言网站,例如 / en/blog、/es/blog 等,因此只需在路由中添加公司名称前缀即可。
Off the top of my head, the routing engine in CakePHP has no concept of domains so what you could theoretically do is map a route eg /company1/post/1 to company1.example.com/post/1 using Apache rewrite
Prefixing in routes is possible as I've used it before to produce a multilingual site eg /en/blog, /es/blog etc so it would be just a case of prefixing the company name in the route.
您正在寻找的是
主题
。CakePHP 1.3 有一个简洁的实现,但是 1.2 可以工作几乎一样好。
在您的 AppController 中,您将需要添加一些内容来根据主机加载不同的主题:
当然,可能性是无限的。您可以通过让每个用户定义多个主题等来从数据库加载
$mapThemes
。但这就是总体思路,我相信这是 Cake 的做事方式。What you are looking for are
themes
.CakePHP 1.3 has a neat implementation of that, but 1.2 works almost as good.
In your AppController, you will want to add something along those lines to load a different theme based on the host:
Of course, possibilities are endless. You can load
$mapThemes
from the database by letting every user define multiple themes, etc. But that's the general idea and what I believe is the Cake way of doing things.