CodeIgniter 用户指南 版本 1.6.3

编辑文档、查看近期更改请 登录注册  找回密码
查看原文

装载类

装载,顾名思义,是用来装载元素。这些元素可以是库 (类) 视图文件助手文件插件, 或者是你自己的文件。

提示: 这个类是由系统初始化的,所以,没有必要自己手动初始化。

以下为这个类里面的函数:

$this->load->library('class_name')

这个函数是用来加载核心类。 class_name 是你要加载的类的名称。 提示: “类”和“库”是可替换使用的。

比如, 你想用 CodeIgniter 来发送邮件, 第一步就是在你的控制器里加载 email 类。

$this->load->library('email');

一但被加载,就可以使用该类了, 使用 $this->email->some_function()。 每一个库文件都在各自的说明文档里被详细地介绍, 所以如果你想使用某个类,就去查看相应的类信息。

参数可以使用数组的形式,作为第二个参数传递给类。

$this->load->view('file_name', $data, true/false)

这个函数是用来加载你的视图文件。 如果你尚未阅读本手册视图 章节的话,建议你先去阅读那里的内容,会有更详细的函数使用说明.

第一个参数是必须的. 指定你要载入的视图文件的名称.  注意: 无需加上 .php 扩展名,除非你使用了其他的扩展名。

第二个参数 optional 允许你传入数组或对象, 传入的数组或对象将使用 php extract 函数导出,你可以在视图中任意使用这些导出的变量. 此外,请阅读 视图 章节了解该功能的更多用法.

The third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:

$string = $this->load->view('myfile', '', true);

$this->load->model('Model_name');

$this->load->model('Model_name');

If your model is located in a sub-folder, include the relative path from your models folder. For example, if you have a model located at application/models/blog/queries.php you'll load it using:

$this->load->model('blog/queries');

If you would like your model assigned to a different object name you can specify it via the second parameter of the loading function:

$this->load->model('Model_name', 'fubar');

$this->fubar->function();

$this->load->database('options', true/false)

This function lets you load the database class. The two parameters are optional. Please see the database section for more info.

$this->load->scaffolding('table_name')

This function lets you enable scaffolding. Please see the scaffolding section for more info.

$this->load->vars($array)

这个函数以一个关联数组作为输入参数,将这个数组用PHP的extract函数, 转化成与这个数组对应的变量.这个函数如果用第二个参数,将产生和上面的$this->load->view()相同的结果 .你之所以要单独用这个函数也许是因为,你想在控制器的构造函数中设置一些全局变量,以使这些变量在任意函数调用的视图(view)里能够用上.你能多次调用这个函数.数组数据被缓存并被并入一个数组,用来转化成变量.

$this->load->helper('file_name')

This function loads helper files, where file_name is the name of the file, without the _helper.php extension.

$this->load->plugin('file_name')

This function loads plugins files, where file_name is the name of the file, without the _plugin.php extension.

$this->load->file('filepath/filename', true/false)

This is a generic file loading function. Supply the filepath and name in the first parameter and it will open and read the file. By default the data is sent to your browser, just like a View file, but if you set the second parameter to true (boolean) it will instead return the data as a string.

$this->load->lang('file_name')

This function is an alias of the language loading function: $this->lang->load()

$this->load->config('file_name')

This function is an alias of the config file loading function: $this->config->load()

 

翻译贡献者: cookieu, Hex, tg8866, weelia
最后修改: 2008-08-25 23:41:28