Welcome Guest, Not a member yet? Register   Sign In
Extending core Controller class
#11

[eluser]gabe[/eluser]
Well I didn't change something in my page controllers but I probably should. What exactly should they look like? I've tried the following which doesn't work:
Code:
<?php

class Home extends MY_Controller {

    function __construct()
    {
        parent::MY_Controller();
        //$this->output->cache( 60 );
    }
    
    function index()
    {
        $this->load->view( 'home' );
    }
}
?>

Which results in errors:
Fatal error: Call to undefined method MY_Controller::my_controller() in /system/application/controllers/home.php on line 7
#12

[eluser]xwero[/eluser]
MY_Controller extends Controller but CI is build to make this extension transparent so you have to extend the Controller for the controller classes that are a part of your site and the added/overloaded method will be available.
#13

[eluser]gabe[/eluser]
So what you are saying is I don't need to make any modifications to my controllers - which is exactly what I thought. If I leave them alone (extending Controller) the thing in the construct of MY_Controller don't work. Sad


Perhaps the best thing for me to do is to is define a method in MY_Controller that does something simple, such as echo "HELLO!". So how would I do that from one of my site controllers? Would it be this (which doesn't work):
Code:
function index()
    {
        $obj =& get_instance();
        $obj->sayHello();
    }
#14

[eluser]xwero[/eluser]
If you have a method in The MY_Controller class you can call it with
Code:
$this->sayHello();
#15

[eluser]gabe[/eluser]
it doesn't work for me, which makes me wonder if my class is even being loaded? I placed a strategic exit() at the top of it and everything worked normally, which makes me belive even more it is being totally ignored.

Is anyone else able to extend Controller in CI 1.6.1 successfully? If so, how do you do it?

Really appreciate all the help from everyone. Thanks.
#16

[eluser]nevsie[/eluser]
I am able to do it using the following:

libraries/MY_Controller.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct access allowed.');

class Public_Controller extends Controller {
    function Public_Controller() {
        parent::Controller();
        $this->output->enable_profiler(TRUE);
    }
    
}

controllers/home.php
Code:
<?php

class Home extends Public_Controller {

    function Home()
    {
        parent::Public_Controller();
    }
    
    function index()
    {
        $this->load->view('home');
    }
}
?>

i wonder if your having either a case issues MY_Controller Vs my_controller (i noticed in one of your errors)
or
if you are causing confusion by using the My_Controller where i am using Public_Controller as the MY_ is setup in config as a prefix special for extending native libraries... Rather than class name... Maybe... Note this is a guess as i am a newbie and still learning.
#17

[eluser]wiredesignz[/eluser]
application/libraries/MY_Controller.php
Code:
class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
    }

application/controllers/main.php
Code:
class Main extends MY_Controller
{
    function Main()
    {
        parent::MY_Controller();
    }
#18

[eluser]gabe[/eluser]
Thanks wiredesignz and nevsie.

I see that you create your new class that extends Controller. Then your site controllers extend your new one. I thought someone said that you didn't need to do that - just declaring a MY_Controller would be enough to override Controller? I thought that was the case having read the user guide. Hmmmppph!
#19

[eluser]wiredesignz[/eluser]
MY_ anything only extends a CI class. If you want to replace it use the same filename as the CI library and put the file in application libraries. The class definition must be the same as the CI original.
#20

[eluser]xwero[/eluser]
Yes i was wrong. I mixed it up with libraries you load using the loader library, i'm sorry.




Theme © iAndrew 2016 - Forum software by © MyBB