在codeigniter安装之外从外部php脚本访问CodeIgniter超级对象
I have been trying so long but couldn't find a solution. For some reason I need to access the codeigniter super object get_instance() from an external php script which is located outside codeigniter installation.
So for example, I have a custom php script called my_script.php inside public_html. And the codeigniter is installed inside public_html/codeigniter.
Followed by the discussion here: http://ellislab.com/forums/viewthread/101620/ I created a file called external.php and put it inside public_html/codeigniter folder, it contains the following code:
<?php
// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
ob_start();
require('./new/index.php');
ob_end_clean();
?>
Then I created a file called my_script.php and put it inside public_html folder (outside codeigniter installation), it contains the following code:
<?php
require('new/external.php');
$ci =& get_instance();
echo $ci->somemodel->somemethod();
?>
Now when I load the my_script.php file from browser, it produces following error:
Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php
If I put the my_script.php file inside codeigniter folder with corrected file path in require() function then it works. But I really need it to work from outside codeigniter installation.
Any idea how to get rid of this problem?
Thanks in advance.
我一直在尝试这么久但找不到解决方案。 出于某种原因,我需要从位于codeigniter安装之外的外部php脚本访问codeigniter超级对象get_instance()。 p>
例如,我在public_html中有一个名为my_script.php的自定义php脚本。 codeigniter安装在public_html / codeigniter中。 p>
其次是讨论: http://ellislab.com / forums / viewthread / 101620 / 我创建了一个名为external.php的文件并将其放在public_html / codeigniter文件夹中,它包含以下代码: p>
&lt; ?php
//删除查询字符串
$ _SERVER ['QUERY_STRING'] ='';
//包含codeigniter framework
ob_start();
require('./ new / index.php');
ob_end_clean();
?&gt;
code> pre>
然后我创建了一个名为my_script.php的文件并将其放在public_html文件夹中(在codeigniter安装之外),它包含 以下代码: p>
&lt;?php
require('new / external.php');
$ ci =&amp; get_instance();
echo $ ci-&gt; somemodel-&gt; somemethod();
?&gt;
code> pre>
现在我加载my_script.php文件 从浏览器,它会产生以下错误: p>
您的系统文件夹路径似乎没有正确设置。 请打开以下文件并更正:index.php p>
blockquote>
如果我将my_script.php文件放在codeigniter文件夹中,并在require()函数中使用更正的文件路径 有用。 但我真的需要它从外部codeigniter安装工作。 p>
知道如何解决这个问题吗? p>
提前致谢。 p>
div>
CI's main index.php
sets the paths of the system and application folders. If you are including index.php
from another directory, these paths are set relative to your "including" directory.
Try changing the following lines in index.php
$system_path = 'system';
// change to...
$system_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'system';
// and...
$application_folder = 'application';
// change to...
$application_folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'application';
The dirname(__FILE__)
will give you the absolute path of the index.php
even if you include it somewhere else.
The problem with loading codeigniter from external file outside codeigniter installation is solved using the first answer which I marked as accepted. And the problem with calling default controller/method was resolved using constant (define). Here is the updated code for external.php file:
<?php
// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
define("REQUEST", "external");
ob_start();
require('./new/index.php');
ob_end_clean();
?>
And here is the default controller method:
public function index($flag = NULL)
{
if (constant("REQUEST") != "external")
{
// some code here
}
}
Thanks very much to contributor hyubs. @hyubs