【发布时间】:2014-06-15 07:41:09
【问题描述】:
我正在尝试了解 MVC 的结构,但我偶然发现了一个地方。
我应该如何集成使用该结构中所有页面的全局功能。
举个例子;
我有一个所有页面都需要的方法,我在侧边栏中显示。
public function user_credit( $userid ){
return $this->db->getSingleRow( "SELECT * FROM user_credit WHERE userid = $userid" );
}
有些页面不需要模型文件。我不想为所有页面创建模型并将此方法添加到所有页面。
我的解决方案;
1 ) 我创建了一个 globalFunc.php 并保存在 helpers 文件夹中。
globalFunc.php
// 扩展模型,因为我需要 db 连接 类 GlobalFunc 扩展模型 {
function __construct(){ parent::__construct(); } public function user_credit( $userid ){ return $this->db->getSingleRow( "SELECT * FROM user_credit WHERE userid = $userid" );}
我在基本控制器中添加 loadHelper 方法。
2 ) 我将 globalFunc.php 保存在 Models 文件夹中。 我在基本控制器中添加了 loadCustomModel 方法。
但是这种方式并没有让我满意。我应该怎么走?
【问题讨论】:
-
“MVC”不是“我的代码”的首字母缩写词。 MVC 模式定义了应用层之间信息流的约束。是否使用函数与 MVC 模式有 NO 关系。此外,模型不是类或表抽象。
标签: php oop model-view-controller helpers