【问题标题】:Common functions in CodeIgniterCodeIgniter 中的常用函数
【发布时间】:2014-07-22 11:55:07
【问题描述】:

我是 CodeIgniter 的新手。

我在 CodeIgniter 中使用 HMVC,并希望在许多其他模块中使用模块函数:

例如,我有一个带有函数 get_locations($param) { return; 的Locaton_model }

如何在许多其他模块中使用上述功能?每次我需要这个函数时我应该在其他模块控制器中加载模型还是在全局的某个地方定义函数?

【问题讨论】:

  • 放入helper文件夹并加载
  • 不,我不能在助手中添加它,因为我的应用程序中有很多这样的功能。如部门、分数、国家、缩写等。以上在许多模块形式中用作下拉列表
  • 您不会为此使用助手。助手是作为“convertArrayToSTring()”的函数

标签: php codeigniter global


【解决方案1】:

您可以通过使用核心控制器轻松实现: http://ellislab.com/codeigniter/user-guide/general/core_classes.html

不要以以下方式开始您的模型:

class Some_model extends CI_Model {}

你开始:

class Some_model extends MY_Model {}

编辑: 也可以使用库: http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

当您想要更一般的东西时,这很有用,比如搜索引擎、IMAP 界面等。

【讨论】:

  • 我在两个模型/模块中有两个函数 get_departments() 和 get_locations。现在我正在添加一个用户配置文件,我需要在其中下拉部门和位置。如何调用这 2 个模型以获取部门和位置列表?
  • 你不能。在 PHP 中,不可能扩展多个类。如果您想在(几乎)每个类中使用该功能,您可以创建一个单核控制器。如果它非常具体,您可能需要使用库。请注意,核心类可以扩展其他核心类。
【解决方案2】:

如果您首先创建自己的模型,请确保它位于核心文件夹内,然后在控制器上扩展模型名称,如下所示

class myController extends Locaton_model {

function index()
{
$this->load->model->("your model name");
$this->yourmodelname->functionname($param);
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 2011-03-09
    • 2019-02-14
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多