cakePHP 项目,文件夹、页面、函数的组织
我对 cakePHP 很陌生,我想知道“实时”网站是如何做到这一点的。
我看到两种可能性:
1)有一个控制器,其(扩展)AppController 中有一堆页面(函数)。
2)有很多控制器,每个控制器在其(扩展)AppController 中都有少量页面(功能)。
(您可能已经得到了我的问题,但我也会以另一种方式说)
我应该将我的联系页面放在与我的博客页面不同的控制器中吗? (我有预感答案是肯定的。)为什么?
Im new to cakePHP, and Im wondering how a 'live' site does this.
I see 2 possibilities :
1) There is one controller with a bunch of pages (functions) in its (extended) AppController.
2) There are many controllers, each with a small number of pages (functions) in its (extended) AppController.
(You probably get my question already, but Im going to say it in another way too)
Should I put my contact page in a separate controller than my blog page? (I have a hunch the answer is yes.) Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要为所有内容创建控制器。事实上,您不应该这样做,因为有更好的方法来解决它。静态页面越多,它就会变得越失控。
对于静态页面
将 Pages_controller.php 从 cake/libs/controller 文件夹复制到您的 app/controllers 文件夹。将以下代码添加到您的display() 操作中:
然后,修改您的routes.php 文件以添加各种静态页面:
现在,联系表单是一个静态页面,但附加了一些逻辑。因此,您可以转到 PagesController 并专门为此(或任何其他不仅仅是静态的页面)创建一个操作:
基本上,该路由将您的静态页面请求定向到 PagesController 的
display()
行动。显示操作检查是否存在同名的方法。如果是,它将执行该操作并显示pages/{page}.ctp 视图。对于非静态页面,例如。博客
现在,这肯定需要一个模型。事实上,有多种模型(帖子有很多评论,帖子 HABTM 标签)。为了操作和访问这些不同的模型,最好将代码放入单独的控制器中。
很多人喜欢根据 URL 来命名他们的控制器。例如,如果他们想要一个诸如
/blog
之类的 URL,他们会将控制器命名为 BlogController。我更喜欢的方法是使用路由来获取我想要的 URL,并按照 CakePHP 约定命名控制器。
例如。 PostsController 将控制 Post 模型和相关模型。但是,如果我希望
/blog
URL 显示所有帖子的列表,我会编写一条路由将其指向/posts/index
。您也可以有其他路线。示例:
/blog/2010/06/10/whats-in-a-post
指向/posts/view/
。再次强调,这只是可能性的一个例子。最后,您应该坚持使用您认为有助于让您和您的团队保持代码井井有条的方法。
You don't need to create a controller for everything. In fact, you shouldn't, because there are better ways around it. The more static pages you have, the more out of hand it can get.
For Static Pages
Copy pages_controller.php from the cake/libs/controller folder over to your app/controllers folder. Add the following piece of code to your display() action:
Then, modify your routes.php file to add the various static pages:
Now, the contact form is a static page, but has some logic attached to it. So, you can head over to your PagesController and create an action specifically for this (or any other page that isn't merely static):
Basically, the route directs your static page request to the PagesController's
display()
action. The display action checks if a method of the same name exists. If it does, it executes that action and displays the pages/{page}.ctp view.For Non-Static Pages, eg. Blog
Now, this definitely needs a model. In fact, multiple models (Post hasMany Comment, Post HABTM Tag). In order to manipulate and access these different models, it's better that you place the code into a separate controller.
A lot of people like to name their controllers based on their URLs. For example, they name their controller as BlogController if they want a URL such as
/blog
.A method that I prefer is using routing to get the URLs that I want, and keeping controllers named as per CakePHP conventions.
Eg. A PostsController would control the Post model and related models. But if I wanted the
/blog
URL to display a list of all the posts, I would write a route to point it to/posts/index
.You can have additional routes too. Example:
/blog/2010/06/10/whats-in-a-post
to point to/posts/view/
.Again, this is just an example of what's possible. In the end, you should stick to the methods that you think helps keep your code organized for both you and your team.