The download, as @yi_H correctly pointed out, lasts the entire current script lifetime. I.E. when you call the controller method, the resource is loaded. If you call the same resource inside another method, it is no longer available.
This is because the controller is initialized with every request, so when you access index.php/mycontroller/method1 controller is initialized (you can turn on the logs and see this clearly). In your method, you load, say, an html helper. If you then access index.php/mycontroller/method2, and you also need the html helper, but you did not load it into the intro method, you will get a function error not found.
So basically, if you want the same resource to always be available, you have 3 options:
- load it into application / config / autoloader.php
- load it with every request, that is, inside each method that this resource will use
- place it inside the controller constructor so that it always initializes with every request.
This is more or less the same as autoload, except that it can only work for the controller in which you put the constructor, so you get the benefit when you don't want something loaded in the EACH controller (for example, when you use autoload), but only on a few. To use this last method, do not forget to WRITE the PARENT CONSTRUCTOR inside your controller (as usual with models):
function __construct() { parent::__construct(); $this->load->library('whateveryouwant'); }
source share