Welcome Guest, Not a member yet? Register   Sign In
How do I subclass one of my own controllers? [SOLVED]
#1

[eluser]CoderReborn[/eluser]
I have a controller named "Done" which extend the Controller class.

I am trying to create a subclass controller called "ToDo" with the following code:
Code:
<?php

class Todo extends Done
{

    public function __construct()
    {
        parent::__construct();
        echo "Todo constructor<br/>";
    }

    public function index()
    {
        echo "Inside Todo index <br/>";
    }

}

?&gt;

However, this causes a blank screen.
When I change "Done" to "Controller" (not what I want), the display is correct.

How do I subclass one of my own controllers?

Thanks!
#2

[eluser]Jelmer[/eluser]
Maybe just like this? (not pretty but probably works)

Code:
&lt;?php

require APPPATH . 'controllers/done.php';

class Todo extends Done
{

    public function __construct()
    {
        parent::__construct();
        echo "Todo constructor<br/>";
    }

    public function index()
    {
        echo "Inside Todo index <br/>";
    }

}

I haven't done this, but I have worked with multiple base controllers. I implemented those by writing an autoloader that would autoload the class after "extends". Thus avoiding needing "require" and it might also be a better solution for you. Write one abstract base controller and then extend both Todo and Done from that base.
#3

[eluser]CoderReborn[/eluser]
Thanks, Jeimer! That works.
#4

[eluser]jwright[/eluser]
[quote author="Jelmer" date="1279839987"]Maybe just like this? (not pretty but probably works)

Code:
&lt;?php

require APPPATH . 'controllers/done.php';

class Todo extends Done
{

    public function __construct()
    {
        parent::__construct();
        echo "Todo constructor<br/>";
    }

    public function index()
    {
        echo "Inside Todo index <br/>";
    }

}

I haven't done this, but I have worked with multiple base controllers. I implemented those by writing an autoloader that would autoload the class after "extends". Thus avoiding needing "require" and it might also be a better solution for you. Write one abstract base controller and then extend both Todo and Done from that base.[/quote]

I like this for a couple reasons.
1. It explicitly shows the file where the base class is located
2. It should work on any CI install even if an autoloader is not setup

That's not to say autoloaders don't have their place. It's nice to not have to worry about these require statements, especially if you would need a lot of them.




Theme © iAndrew 2016 - Forum software by © MyBB