“用户控制”使用 MVC PHP
我想知道如何在 MVC 框架(特别是 CodeIgniter)中使用 PHP 实现 asp.net“用户控件”之类的功能。
为了更好地解释我想要什么,这里有一些指导:
.net 用户控件是一个文件,包含 aspx 和代码 (c#/vb),它在实现它的每个页面(例如,购物车)中提供功能。它可以轻松添加到母版页中,母版页是站点中所有其他页面的容器。
在MVC结构中,页面由控制器加载,控制器加载视图。我知道我可以在视图中加载视图,但是如何为“跨站点视图”提供控制器代码而不在每个主控制器中重复它?
示例:我有一个视图,将数据库中的类别加载到选择列表中,并且该视图位于每个页面的顶部。由于我不应该(而且我不确定是否可能)从视图访问我的类别模型,因此我应该在哪里放置代码来加载此数据,而不必在每个控制器的每个函数中重复它?
I was wondering how to achieve an asp.net "user control" like functionality with PHP in a MVC framework, specifically CodeIgniter.
To explain better what I want, here's some guidance:
A .net user control is a file, with both aspx and code (c#/vb) that provides a functionality across every page that implement it (say, a shopping cart). It is easily added to a Master Page which is a container for every other pages in the site.
In MVC structure, a page is loaded by the controller, which loads the View. I understand I can load Views inside Views, but how to provide the controller code for "cross site views" without repeating it every main controller?
Example: I have a view that loads Categories from database into a select list and this view is on top of every page. As I shouldn't (and I'm not sure if it is even possible) access my Category model from the view, where do I put the code to load this data without having to repeat it in every function in every controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想避免在每个控制器中重复代码,那么只需扩展
CI_Controller
类并在实例化时在新的 Controller 类中进行所有设置...并让所有普通控制器继承自 <代码>New_Controller。If you want to avoid duplicating code in each controller, then simply extend the
CI_Controller
class and do all of the setup in your new Controller class upon instantiation ... and have all of your normal controllers inherit fromNew_Controller
.正如 Sean 所说,如果功能位于控制器级别,您应该扩展 CI_Controller。
可能某些功能应该在视图级别,因此加载器视图可用于帮助您保持每个页面的全局布局并包含常见功能。类似于:
[view] layout.php:
在您的控制器中,您应该始终加载此视图并在参数数组中传递内容真实视图的值,例如
As Sean said, if the functionality is at Controller level you should extend CI_Controller.
May be some functionality should be at view level, so a loader view can be used to help you keep the global layout of every page and include the common features. that's something like:
[view] layout.php:
In your controller you should always load this view and pass in the parameter array the value of the contents real view, like in
我很少使用母版页,主要是在 Sharepoint 中,但如果您想要一个
front.master
页面,可以这么说,然后加载该上下文中的所有其他页面,您最好的选择是创建一个新库作为您的视图处理程序并以编程方式设计它以加载主视图中的辅助视图。一个简单的类,包括:
这就是您正在寻找的东西吗?
I have worked very little with master pages, mainly in Sharepoint but if you want a
front.master
page so to speak and then load all other pages within that context your best bet would be to create a new library as your view handler and pro grammatically design it to load the secondary view within the master.A simple class consisting of:
IS this the kind of thing you was looking for.
回答我自己的问题:我发现了一些值得检查的东西。它提供了多个 MVC 三元组: https://bitbucket.org/wiredesignz/codeigniter -modular-extensions-hmvc/wiki/Home
Answering to my own question: i found something worth checking. it provides Multiple MVC triads: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home