Reputation: 378
I have created a function in application/helpers/ which works fine when accessing from ci. Now I want to call the same function from outside ci i.e., from core PHP file. But I'm not able to do so.
Below is the approach tried.
Created a test.php outside ci and below is the code:
<?php
$filepath = dirname(__FILE__);
ob_start();
require_once($filepath.'/ci/index.php');
ob_get_clean();
return $CI;
?>
In another core PHP file, test2.php, below is the code:
$CI = require_once('test.php');
echo $CI->config->item('base_url');
some_helper_function($param1, $param2);
Error message:
Fatal error: Call to a member function item() on a non-object in <path>/Utf8.php on line 47
Folder structure:
test.php
test2.php
ci/application/helpers/test_helper.php (contains some_helper_function())
Any suggestions?
Upvotes: 1
Views: 960
Reputation: 315
Create a file and put the following code into it.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('test_function'))
{
function test_function()
{
//your functionality//
}
}
Save this to application/helpers/ . save it as "test_helper.php"
The first line exists to make sure the File cant be included and run from outside the Code Igniter.
Then in your controller or Model
$this->load->helper('test_helper');
You can use any function from that helper page.
Else
your-project-name\application\config\autoload.php
$autoload['helper'] = array('test_helper');
So that you can use that helper function in any controller or model. Not to initialize in each and every controller or model like previously said.
Upvotes: -1