我应该将实际的控制器文件命名为与 Codeigniter 中的视图文件相同的名称吗?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现的唯一烦人的事情是 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.控制器和型号通常具有相同的名称。这使得您的主控制器可以轻松地根据特定的页面名称调用正确的控制器和模型。
在大多数情况下,使用相似名称是合乎逻辑的,例如多个视图。
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.
我在编写 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.