0

I have an issue with my codeigniter helper.

This is my controler that calls a view:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $this->load->view('inc/header');
        $this->load->view('login_view');
        $this->load->view('inc/footer');
    }
}

This works fine, but I have more then one controler with multiple functions that can call a view.

so I thought I make a helper:

function view($site) {
    $this->load->view('inc/header');
    $this->load->view($site);
    $this->load->view('inc/footer');
}

When I call this helper like this:

view('login_view');

I get this error:

> Fatal error: Using $this when not in object context in C:\.....\application\helpers\custom_helper.php on line 23

line 23 =

$this->load->view('inc/header');

What's wrong with this code?

1 Answer 1

1

When building helpers, you must use the get_instance function. This allows your function to use the CodeIgniter resources.

So, your function would look like this;

function view($site)
{
    $CI =& get_instance();

    $CI->load->view('inc/header');
    $CI->load->view($site);
    $CI->load->view('inc/footer');
}

EDIT:

You should also pass an array to this function, which will allow you to load data to the view. Like this;

function view($site, $data = array())
{
    $CI =& get_instance();

    $CI->load->view('inc/header', $data);
    $CI->load->view($site, $data);
    $CI->load->view('inc/footer', $data);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You should also pass an array through to the view function, for any dynamic data you want to pass to the view. I will update my answer with a new function to show you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.