CodeIgniter Forums
Extending Controller & _output() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Extending Controller & _output() (/showthread.php?tid=1989)



Extending Controller & _output() - El Forum - 07-10-2007

[eluser]Unknown[/eluser]
I'm new to CI.

I want to extend the main Controller so that a function will be included in all of my controllers.
My problem is the view within views. I've found a view library that seems to do the job i want. But I don't want to have to keep doing
Code:
$this->view->part('left_column', 'left_column.php');  // (file extensions added for clarity)
$this->view->part('right_column', 'right_column.php');
...
$this->view->load('main_template');

in every controller.
So I wanted to put a _output function in every controller by default. If I put the _output function in every controller, it works fine. But again, it's having to put the same code in every page.

Which is where extending the base controller would come in.
However, when I tried to extend the controller using
Code:
<?php

class MY_Controller extends CI_Controller
{
    function _output($string)
    {
        echo "_output()<br/>";
    }
}
?&gt;
in system/application/libraries/MY_Controller.php I get
Quote:Fatal error: Class 'CI_Controller' not found in C:\Documents and Settings\Luke\My Documents\Web Sites\dating\index\system\application\libraries\MY_Controller.php on line 3
When I change "CI_Controller" to "Controller" it works fine. But it doesn't call the _output function.

I suppose I could just create a normal output function and include it as a helper or something and then pass the controller through as an object, but it's not very elegant.

My end plan is to run the controller as normal and then load a single particular view (eg login/register page). Once the view is loaded and processed via normal methods, the _output function takes over and inserts the view loaded into the correct place in a 'global' template/view (eg header tags, navigation bars).

Anyone have any pointers on how to do this?


Extending Controller & _output() - El Forum - 07-10-2007

[eluser]Mirage[/eluser]
There's probably a couple of ways to work this. The controller, model and database classes are not officially supported for the Core Class extension.

An option you do have is to include your own controller subclass:

Code:
&lt;?php

class BaseController extends Controller {

    function BaseController () {
        parent::Controller();
    }

    function _output () {
        // your output code here
    }
}
?&gt;


Code:
&lt;?php

require_once('APPPATH'.'controllers/BaseController.php');

class SomeController extends BaseController {

    function SomeController () {
        parent::Controller();
    }

    function index () {
        // do something
    }
}
?&gt;


However, I don't think subclassing the controller is the best solution for your problem, because the problem isn't controller related but rather view related as you stated. I therefore recommed you use that view library from coolfactor. You could subclass that and then always add the default parts to it in it's constructor. This gives you the option to still override them from the controller or a specific controllers function.

Still, I'd personally opt to set the 'parts' in each controllers constructor. It's a default thing that can be overridden in the action methods.

Cheers!