Codeigniter: Change autoload libraries for one controller?

别说谁变了你拦得住时间么 提交于 2019-12-11 11:03:28

问题


I've the following libs in the autoload.php:

$autoload['libraries'] = array('database', 'session');

I need the session lib everywhere - besides of ONE controller, where it causes only trouble. Now the question is:

Is there a possibility NOT to load the session lib for this special controller or is the only option to delete it from the autoload and add it to every single controller, where needed?


回答1:


In your autoload.php:

$autoload['libraries'] = array('database');

$CI =& get_instance();
if ($CI->router->fetch_class()!='conflictingcontroller') array_push($autoload['libraries'], 'session');



回答2:


Taken from this answer: Link

Autoloading is meant for site-global items.

A cleaner solution may be to extend the controller and load the library in that new controller's constructor. Then all of your controllers extend from that controller, except the one(s) you don't want to load that library - those can extend the original CI controller.

That should take you < 5 minutes to implement and you won't have to hack anything.

However since it is a session and not database or uri helpers, Kumar's answer may work for you.




回答3:


You can do as below in your controller constructor

$this->session = null; 

or

unset($this->session);


来源:https://stackoverflow.com/questions/21577592/codeigniter-change-autoload-libraries-for-one-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!