cakePHP 项目,文件夹、页面、函数的组织

发布于 2024-10-05 18:18:48 字数 256 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2024-10-12 18:18:48

您不需要为所有内容创建控制器。事实上,您不应该这样做,因为有更好的方法来解决它。静态页面越多,它就会变得越失控。

对于静态页面

将 Pages_controller.php 从 cake/libs/controller 文件夹复制到您的 app/controllers 文件夹。将以下代码添加到您的display() 操作中:

function display() {

    ...

    $page = Inflector::slug($page);
    if (method_exists($this, $page)) {
        $this->$page();
    }

    $this->render(join('/', $path));
    return;
}

然后,修改您的routes.php 文件以添加各种静态页面:

Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
Router::connect('/contact', array('controller' => 'pages', 'action' => 'display', 'contact'));

现在,联系表单是一个静态页面,但附加了一些逻辑。因此,您可以转到 PagesController 并专门为此(或任何其他不仅仅是静态的页面)创建一个操作:

function contact() {
    if (!empty($this->data)) {
        ...
    }
}

基本上,该路由将您的静态页面请求定向到 PagesController 的 display() 行动。显示操作检查是否存在同名的方法。如果是,它将执行该操作并显示pages/{page}.ctp 视图。

对于非静态页面,例如。博客

现在,这肯定需要一个模型。事实上,有多种模型(帖子有很多评论,帖子 HABTM 标签)。为了操作和访问这些不同的模型,最好将代码放入单独的控制器中。

很多人喜欢根据 URL 来命名他们的控制器。例如,如果他们想要一个诸如 /blog 之类的 URL,他们会将控制器命名为 BlogController

我更喜欢的方法是使用路由来获取我想要的 URL,并按照 CakePHP 约定命名控制器。

例如。 PostsController 将控制 Post 模型和相关模型。但是,如果我希望 /blog URL 显示所有帖子的列表,我会编写一条路由将其指向 /posts/index

Router::connect('/blog', array('controller' => 'posts', 'action' => '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:

function display() {

    ...

    $page = Inflector::slug($page);
    if (method_exists($this, $page)) {
        $this->$page();
    }

    $this->render(join('/', $path));
    return;
}

Then, modify your routes.php file to add the various static pages:

Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about'));
Router::connect('/contact', array('controller' => 'pages', 'action' => 'display', 'contact'));

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):

function contact() {
    if (!empty($this->data)) {
        ...
    }
}

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.

Router::connect('/blog', array('controller' => 'posts', 'action' => '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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文