codeigniter的负载如何工作? [英] How does codeigniter's load work?

查看:114
本文介绍了codeigniter的负载如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦了解如何codeigniters加载工作。

I'm having some trouble understanding how codeigniters loading works.

首先你有自动加载,看起来很直接,它加载一切每一次。所以这听起来很好用的东西我用的所有的时间。

Well first you have the autoload which seems pretty straight forward, it loads everything everytime. So this sounds good to use for the stuff I use all the time.

第二,你可以加载所有内联。但这里是我的问题:它保持加载多长时间?

Second you can load everything inline. But here is my question: How long does it stay loaded?

让我说在控制器中加载表单验证库,然后加载模型,我可以使用在模型中的表单验证或者我必须重新加载它吗?
继续让我说我加载一个视图和另一个控制器,我可以使用表单验证吗?还是我需要加载?
重定向后?
如果我加载模型或帮助程序而不是库?
假设我想在另一个模型中使用一个模型,我在哪里加载这个模型?

Let say I load the form validation library in the controller, then I load the model, can I use the form validation in the model or do I have to reload it again? Continuing let say I load a view and another controller, can I use the form validation? Or do I need to load? After a redirect? How about if I load a model or helper instead of a library? Let say I want to use a model inside another model, where do I load that one?

所以基本问题,多长时间,

So the basic question, how long or rather how far does the load go before I need to reload?

推荐答案

正如@yi_H正确指出的,加载会持续到当前脚本一生。 I.E.当你调用控制器的方法时,资源被加载。如果你在另一个方法中调用相同的资源,那就不再可用了。

The loading, as @yi_H correctly pointed out, lasts for all the current script lifetime. I.E. when you're calling a controller's method, the resource is loaded. If you call the same resource inside another method, that isn't available anymore.

这是因为控制器在每个请求都被初始化,所以当你访问
index.php / mycontroller / method1 初始化控制器(您可以启用日志,并清楚地看到)。在你的方法中,加载,例如,html助手。
如果你然后访问 index.php / mycontroller / method2,,它还需要html助手,但你没有加载它的方法,你会

That happens because controller are initialized at each request, so when you access index.php/mycontroller/method1 the controller is initialized (you can enable logs and see this clearly). In your method you load, say, the html helper. If you then access index.php/mycontroller/method2, and it also requires the html helper, but you didn't load it intro the method, you will get an error of function not found.

因此,基本上,如果你想有相同的资源总是可用,你有3个选择:

So, basically, if you want to have the same resource always available you have 3 choices:


  1. 在应用程式/ config / autoloader.php中自动载入

  2. 在每个要求载入,即在每个要使用资源

  3. 将它放在控制器的构造函数中,以便在每次请求时始终初始化它。

它或多或少与自动加载相同,只是它只能用于你把构造函数放在控制器,所以你会得到一个好处,当你不想要的东西加载在每个控制器(如当你使用自动加载),但只有几个。
为了使用这最后一种方法,记得在你的控制器里面调用父母构造器(像你通常用模型一样):

It's more or less the same as autoloading, except that it can work only for the controller which you put the constructor in, so you get a benefit when you don't want something to be loaded at EACH controller (like when you use autoloading) but only on a few. In order to use this last method, remember to CALL THE PARENT CONSTRUCTOR inside your controller (like you do normally with models):

function __construct()
{
  parent::__construct();
  $this->load->library('whateveryouwant');
}

这篇关于codeigniter的负载如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆