我应该将实际的控制器文件命名为与 Codeigniter 中的视图文件相同的名称吗?

发布于 2024-09-26 12:52:24 字数 352 浏览 4 评论 0原文

我正在使用 MVC 模式编写我的第一个 Codeigniter 站点。我正在构建一些现在加载视图的控制器(尚未进入模型),但我注意到我的视图和控制器文件具有相同的文件名(如 products.php)。当然,它们位于各自的文件夹中。例如,我有一个 About 控制器,它加载一个 About 视图,这两个视图都被命名为 about.php。我有一个产品控制器,它加载一个产品视图,它们都名为 products.php。这是好的做法吗?

通过阅读和研究,似乎每个人都以不同的方式命名他们的模型,例如 Products_Model.php,这使得很容易区分,但我不记得看到任何人对控制器和视图做任何不同的事情。

当这个网站进一步发展时,我是否会感到头疼?谢谢你!

I'm writing my first Codeigniter site using the MVC pattern. I'm building some controllers that load views now (haven't gotten to Models yet) but I'm noticing that my View and Controller files have the same filename (like products.php). They are in their respective folders of course. For example I have an About controller, that loads an About view, both of which are named about.php. I have a Products controller that loads a Products view which are both named products.php. Is this good practice?

From reading and studying it seems everyone names their models differently, like Products_Model.php, which makes it easy to distinguish, but I can't recall seeing anyone doing anything different with the controllers and views.

Am I setting myself up for a headache down the road when this site develops more? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

单挑你×的.吻 2024-10-03 12:52:25

我发现的唯一烦人的事情是 IDE 中的选项卡都标记为“products.php”,并且很难区分它们。我现在倾向于将 '_view' 添加到视图文件的末尾。只做感觉正确的事。以后更改文件名并不难。

The only annoying thing I find is the tabs in your IDE are both marked 'products.php' and it's hard to tell them apart. I tend to tack '_view' onto the end of the view files now. Just do what feels right. It's not that hard to change filenames later on.

2024-10-03 12:52:25

控制器和型号通常具有相同的名称。这使得您的主控制器可以轻松地根据特定的页面名称调用正确的控制器和模型。

在大多数情况下,使用相似名称是合乎逻辑的,例如多个视图。

application/
  controller/
    customers.controller.php
    products.controller.php
  model/
    customers.model.php
    products.model.php
  view/
    customers_list.php
    customers_add.php
    products_list.php
    products_export.php

The controller and model usually have the same name. This makes it easy for your main controller to call on the correct controller and model depending on a certain page name.

And in most cases it's logical to use similar names, e.g. with multiple views.

application/
  controller/
    customers.controller.php
    products.controller.php
  model/
    customers.model.php
    products.model.php
  view/
    customers_list.php
    customers_add.php
    products_list.php
    products_export.php
倾其所爱 2024-10-03 12:52:24

我在编写 CodeIgniter 站点时所做的就是在我的视图中创建一个与控制器名称相同的文件夹

,如果我有一个名为 index() 的方法,它将查看:/views/controller_name/index.php。

在你的控制器中,你可以这样做: $this->load->view('controller_name/index');
这样您就可以加载文件夹内的文件。

What I do when writing CodeIgniter sites is creating a folder same as the controller name in my views

And if I have a method called index(), it'll view: /views/controller_name/index.php.

In your controller you can do this: $this->load->view('controller_name/index');
so you can load the file inside the folder.

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