1

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?

1 Answers1

-1

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.

imlokeshs
  • 195
  • 1
  • 1
  • 10
  • Here you have written the code on how to call helper function in controller/model. My requirement is to call the helper function outside the ci. – Kalyan Srinivas Limkar Dec 20 '17 at 11:52
  • Just include your helper page in which page you want to use that helper page function like given below like include('./application/helpers/test_helper.php'); and use that function in that page. – imlokeshs Dec 20 '17 at 11:57
  • $CI = require_once 'CI.php'; echo $CI->config->item('base_url'); // test CI instance /*Include like this and try */ – imlokeshs Dec 20 '17 at 12:12
  • https://codeinphp.github.io/post/codeigniter-tip-accessing-codeigniter-instance-outside/ – imlokeshs Dec 20 '17 at 12:15
  • My approach is from the above link (https://codeinphp.github.io/post/codeigniter-tip-accessing-codeigniter-instance-outside/). It's not working. – Kalyan Srinivas Limkar Dec 20 '17 at 12:18