Welcome Guest, Not a member yet? Register   Sign In
Calling a Controller Function from an External PHP File?
#1

[eluser]Vik[/eluser]
Is there a way to call a function in a CI controller, from a PHP file that is not part of the CI app?

Note - this is part of my effort to integrate a Simple Machines Forum board, with my CI app.

Thanks in advance to all for any info.
#2

[eluser]gunter[/eluser]
maybe you can try this, but I have never heard of someone who has used it
http://codeigniter.com/wiki/dip_into_CI/
#3

[eluser]Vik[/eluser]
I'm going to see if the code from this page does the trick - [email=http://www.bin-co.com/php/scripts/load/]PHP Load function[/email].
#4

[eluser]Negligence[/eluser]
Try this.

Code:
include('path/to/controller.php');
$controller = new Controller();

call_user_func(array($controller, 'function_name'));
#5

[eluser]Vik[/eluser]
Thanks for the suggestion, Negligence. This will be cool if it works.

I have to include a lot of stuff to define the controller class. Here's what I've got so far:

Code:
$system_folder = "system";
define('BASEPATH', $system_folder.'/');

if (floor(phpversion()) < 5)
{
    load_class('Loader', FALSE);
    require(BASEPATH.'codeigniter/Base4.php');
}
else
{
    require(BASEPATH.'codeigniter/Base5.php');
}

include ('system/libraries/Controller.php');
include ('system/application/controllers/home.php');
$controller = new Controller();
call_user_func(array($controller, 'login_via_smf'));

On the line:

Code:
$controller = new Controller();

...I'm getting the following error:

Quote:Debug Error: system/libraries/Controller.php line 74 - Call to undefined function load_class()

I.e. there's a call to load_class in Controller.php, line 74, that PHP says is undefined. (I'm using PHP 5, so the call to load_class in my code isn't getting executed.)

Where does function load_class get defined? I can't find it yet in the PHP manual _or_ the CI manual! Smile
#6

[eluser]Negligence[/eluser]
It's in system/codeigniter/Common.php
#7

[eluser]Vik[/eluser]
It just occurred to me that, even if I get this approach working, I won't have access to all the variables produced when CI is run, because, I won't have actually started CI - I'll just be running a class method.

Hmmm......

It looks like FreakAuth author Grahack has developed an approach to this, which he describes in this forum thread.




Theme © iAndrew 2016 - Forum software by © MyBB