简体   繁体   中英

Get CodeIgniter instance outside a controller context

I have two apps running at same server and one of them is CI. I need the one that is not CI to use models, libraries, helpers etc. So I needed something like get_instance() , but I'm not in a controller's context, I'm outside CodeIgniter.

How can I get a generic instance of a controller, or instantiate models or import helpers from a different application?

Example: A and CI are apps and both runs on the same server

ROOT
  A
     script.php
  CI (it's a CodeIgniter project, with controllers, models etc)
  ...
     random_model.php
  ...
     random_helper.php

How can I make script.php on A use random_model.php on CI?

Nice question, but I'm afraid that you can't do that, and even if we assume you can do it, this will do more damage than good, for example you call a custom library that is using the session library, then your application "A" will implicitly use that session library and can have unpredictable consequences,mostly if "A" is using native php session.

This is not only true for session but other configuration settings too (database, base_url,charset,...and other settings) and it will auto load other libraries, helpers, packages ...etc if any from your autoload.php

a better way is to create a controller under the "codeigniter" project and call it as you usually do with other controllers.

or even better, create a second codeigniter project, since it has a very small footprint, for your script "A" and avoid all conflict between the two projects and get more organized

I hope that makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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