Tuesday, January 29, 2013

Codeigniter Bonfire error: "Cannot redeclare function in a view file"

Well, that was annoying.

Background

I'm working with Codeigniter-Bonfire to create a web application. CI-Bonfire is an MVC framework that lets you put views within views by calling modules::run('path/to/controller/function', $arg).

Error

I kept getting this error whenever I tried to call modules::run on a function that I know works.

Fatal error: Cannot redeclare function_name() (previously declared in /path/to/view_file.php:2) in /path/to/view_file.php on line 15.

Line 2  in view_file.php is where I declare the function. Line 15 is where the function ends. I know this error comes up because I'm using view_file.php twice somehow and that's why the function is getting redeclared. But where??

Back to the controller I go and I noticed that I forgot a simple else statement.

In CI-Bonfire, you can print out a view either by itself (using the standard base_url/module/controller/function/args path) or by calling $this->load->view('view_folder/view_file', $args_array) in the controller if you've set some variable to true.

My rendering function looked like this:

if ($hmvc)
$this->load->view('view_folder/view_file', $args_array);
Template::render();

But it should've looked like this:

if ($hmvc)
$this->load->view('view_folder/view_file', $args_array);
else
Template::render();

Phew!