• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access codeigniter super object from external files

#1
[eluser]helmutbjorg[/eluser]
Hi Guys,

I think I am getting closer to a simple way to access the CI super object from an external script. Sometimes you need the methods inside third party scripts (like forums, wikis, fckeditor filemanager, etc) to authenticate, etc.

My idea is to simply include the main index.php inside phps output buffering somehow... For example we have a script

external.php
Code:
ob_start();
include('index.php');
ob_end_clean();
$CI =& get_instance();
// And now you can use the methods, etc

I have hit a problem which I think there must be a solution to. It works perfectly if I do not echo anything to the screen. The only time it doesn't work is when I have Output Compression set to true in the config.php and I echo something. If I don't output anything it works great with compression on. I would like this to work with this setting true or false. The error I get is the standard browser error 'Content Encoding Error'.

Anyone have any ideas?

I know that other people have had other ideas to fix this general problem (such as Dip Into CI http://codeigniter.com/wiki/dip_into_CI/) etc but I think if this works it is a VERY simple solution.

Cheers Steve

#2
[eluser]helmutbjorg[/eluser]
An update...

At this stage this system works with the following conditions...
a) Must have Output Compression set to false
b) Must not be using CodeIgniter with GET vars enabled (by default they are off)

How to use
1. Create file called external.php (or whatever) with the following code
Code:
<?php
// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
ob_start();
include('index.php');
ob_end_clean();
?>
2. In your external scripts you use it like so...
Code:
require('external.php');
$CI =& get_instance();
// And now you can use the methods, etc

#3
[eluser]whobutsb[/eluser]
Thank you for posting this method on how to access the CI Super Object. I'm having a slight problem with using it though. What I'm trying to do is be able to access CI libraries and resources in Wordpress.

My setup is as follows:
Code:
CI Root
  -public (images, js, css)
  -system (CI Application)
  -wordpress
  external.php (your external file, from the documentation from above)
  index.php (CI index file)


From what I can understand from your tutorial I have the following settings set in my CI config:

Quote:$config['enable_query_strings'] = TRUE;
$config['compress_output'] = FALSE;

And in the header of my Wordpress Theme I have this added:
Code:
<?php
require('../external.php');
$CI =& get_instance();
/**
* @package WordPress
* @subpackage Default_Theme
*/

Whenever I load the blog I get the error message:
Code:
Fatal error: Call to undefined function get_instance() in C:\wamp\www\Green\wp-content\themes\default\header.php on line 3

Am I missing something from your instructions?

#4
[eluser]123wesweat[/eluser]
and did you get this to work i am also trying to

<?php $this->load->view('from_ci_view');?>

in a WP theme

#5
[eluser]123wesweat[/eluser]
[quote author="helmutbjorg" date="1231406839"]An update...


2. In your external scripts you use it like so...
Code:
require('external.php');
$CI =& get_instance();
// And now you can use the methods, etc
[/quote]

could you give me an example how you use the CI load views in WP?

regards

#6
[eluser]Phil Sturgeon[/eluser]
Code:
<?php $this->load->view(‘from_ci_view’);?>

$this refers to the current object. You should be using:

Code:
$ci =& get_instance();
$ci->load->view('from_ci_view');

#7
[eluser]123wesweat[/eluser]
ha that´s looks promising

But i get an error on the $CI =& get_instance();

Code:
Fatal error: Call to undefined function get_instance() in /var/www/vhosts/domain.com/blog/wp-content/themes/test/header.php on line 13

in header.php i have
Code:
require_once('../external.php');
$CI =& get_instance();
$CI->load->view('from_ci_view');


in external.php i have
Code:
¨// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
ob_start();
include('index.php');
ob_end_clean();


btw i have added
Code:
require_once './blog/wp-load.php';//WP
to index.php
so i can use WP functions in CI views.


ps http://wiki.title.cz/user_guide/general/...aries.html


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.