简体   繁体   English

在CodeIgniter的模型文件中加载库文件

[英]Load a library file in a model file in CodeIgniter

I want to load library file from model in codeigniter. 我想从Codeigniter中的模型加载库文件。 Can anyone please help me. 谁能帮帮我吗。

You should always load the libraries in controller,as models are always called from controllers. 您应该始终将库加载到控制器中,因为总是从控制器调用模型。

Anyways you can try like this: 无论如何,您都可以这样尝试:

$this->load->library('library_name');
$lib= new library_name();
$lib->somemethod();

Put your file in the library folder and use the following code to load it in the controller: 将文件放在库文件夹中,并使用以下代码将其加载到控制器中:

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

Remember, do NOT put your code in the system folder's library folder. 请记住,不要将您的代码放在系统文件夹的库文件夹中。 Put it in the appilcation's library folder. 将其放在应用程序的库文件夹中。

Once loaded, you can use the classes defined in your library. 加载后,您可以使用库中定义的类。 Remember, libraries work different than models. 请记住,库的工作方式不同于模型。 For Example: 例如:

Your library: <? class Foo{ Foo() {} function Bar() {} } ?> 您的图书馆: <? class Foo{ Foo() {} function Bar() {} } ?> <? class Foo{ Foo() {} function Bar() {} } ?> saved as myFoo.php in application/library/ <? class Foo{ Foo() {} function Bar() {} } ?>另存为myFoo.phpapplication/library/

Load it like $this->load->library('myFoo'); $this->load->library('myFoo');一样加载它$this->load->library('myFoo'); then use it like $foo = new Foo(); $bar = $foo->Bar(); 然后像$foo = new Foo(); $bar = $foo->Bar();一样使用它$foo = new Foo(); $bar = $foo->Bar(); $foo = new Foo(); $bar = $foo->Bar();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM