在codeigniter安装之外从外部php脚本访问CodeIgniter超级对象

时间:2014-05-27 01:08:59

标签: php codeigniter

我一直在努力但却无法找到解决方案。出于某种原因,我需要从位于codeigniter安装之外的外部php脚本访问codeigniter超级对象get_instance()。

例如,我在public_html中有一个名为my_script.php的自定义php脚本。 codeigniter安装在public_html / codeigniter中。

接下来的讨论:http://ellislab.com/forums/viewthread/101620/我创建了一个名为external.php的文件并将其放在public_html / codeigniter文件夹中,它包含以下代码:

<?php
// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
ob_start();
require('./new/index.php');
ob_end_clean();
?>

然后我创建了一个名为my_script.php的文件并将其放在public_html文件夹中(在codeigniter安装之外),它包含以下代码:

<?php
require('new/external.php');
$ci =& get_instance();
echo $ci->somemodel->somemethod();
?>

现在,当我从浏览器加载my_script.php文件时,会产生以下错误:

  

您的系统文件夹路径似乎未正确设置。请打开以下文件并更正:index.php

如果我将my_script.php文件放在codeigniter文件夹中并且在require()函数中使用了更正的文件路径,那么它就可以了。但我真的需要它从外部codeigniter安装工作。

知道如何摆脱这个问题吗?

提前致谢。

2 个答案:

答案 0 :(得分:3)

CI的主index.php设置系统和应用程序文件夹的路径。如果您从另一个目录中包含index.php,则这些路径将相对于您的&#34;包括&#34; 。目录

尝试更改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';

dirname(__FILE__)会为您提供index.php的绝对路径,即使您将其包含在其他地方也是如此。

答案 1 :(得分:2)

使用我标记为已接受的第一个答案解决了从codeigniter安装之外的外部文件加载codeigniter的问题。调用默认控制器/方法的问题是使用常量(define)解决的。以下是external.php文件的更新代码:

<?php
// Remove the query string
$_SERVER['QUERY_STRING'] = '';
// Include the codeigniter framework
define("REQUEST", "external");
ob_start();
require('./new/index.php');
ob_end_clean();
?>

这是默认的控制器方法:

public function index($flag = NULL) 
 {
  if (constant("REQUEST") != "external")
  {
    // some code here
  }
 }

非常感谢贡献者hyubs。 @hyubs

相关问题