我试图从一个common_helper.php文件调用核心php文件中的一个函数,该文件位于codeigniter的application / helpers文件夹中。辅助文件中的wrtten函数也用于成员控制器。所以在codeigniter网站中,这是有效的。但不是在外面的核心php.Please帮助我
文件结构:
项目/ test.php的
应用/控制器/会员
应用/助手/ common_helper.php
这是我的common_helper.php
function reg_code()
{
$CI =& get_instance();
echo "in registration_code";
$registration_code = get_reg_code();
return $registration_code;
}
function check_code($registration_code)
{
$CI =& get_instance();
$sql = "SELECT * FROM membership WHERE reg_code = '$registration_code'";
$query = $CI->db->query($sql);
if($query->num_rows() > 0){
return FALSE;
}else{
return TRUE;
}
}
function get_reg_code()
{
$CI =& get_instance();
$CI->load->helper('string');
$alha_result = random_string('alpha',1);
$numeric_result = random_string('numeric',3);
$reg_code = $alha_result.$numeric_result;
$reg_code = strtoupper($reg_code);
//check if code exists
if(check_code($reg_code)){
return $reg_code;
}else{
get_reg_code();
}
return $reg_code;
}
test.php的
include_once "../application/helpers/common_helper.php";
$reg = reg_code();
echo "Reg Code :".$reg;
答案 0 :(得分:0)
您使用$ CI =& get_instance();在你的帮助函数中 在这个调用之后,在$ CI变量中有主框架类对象,这就是为什么当你在Controller中使用它时一切正常。但是当你在核心php文件中使用它时,你没有初始化框架类,所以方法调用get_instance()失败并且没有任何工作。
答案 1 :(得分:0)
我的自定义助手 - (custom_helper.php)
<?php
if(!function_exists('p'))
{
function p($arr)
{
echo '<pre>';
print_r($arr);
echo '</pre>';
die;
}
}
?>
然后我在项目根目录
中创建了i sample.php文件sample.php -
<?php
include('./application/helpers/custom_helper.php');
$arr = array('1','2','3','4');
p($arr);
?>
输出 -
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
请使用这种方式,它将100%工作,并且不使用帮助文件中的以下行,我们可以访问应用程序文件夹之外的帮助方法。
if ( ! defined('BASEPATH')) exit('No direct script access allowed');