Welcome Guest, Not a member yet? Register   Sign In
Extending from my own controller
#1

[eluser]Agustín Villalba[/eluser]
Hi! Now I'm trying to extend a controller of mine which extends from CodeIgniter controller, just like this:

Controller1.php
class Controller1 extends Controller {
function Controller1(){
parent::Controller();
}
... }

Controller2.php
class Controller2 extends Controller1 {
function Controller2(){parent::Controller1();}

This code returns this error:
Fatal error: Class 'Controller1' not found in C:\xampplite\htdocs\project\system\application\controllers\Controller2.php on line XX

Can any one tell me how do I extend a controller from another one?? Thank you very much!!
#2

[eluser]maria clara[/eluser]
i haven't tried it yet but try to read the CodeIgniter User_guide. interesting to know that issue.
#3

[eluser]Agustín Villalba[/eluser]
I have read it yet, and doesn't mention anything about this situation.
#4

[eluser]n0xie[/eluser]
If you are running PHP5, try this:
Code:
// add to application/config/config.php
function __autoload($class)
{
    if (file_exists(APPPATH."controllers/".$class.EXT))
    {
        require_once(APPPATH.'controllers/'.$class.EXT);
    }
}
#5

[eluser]tomcode[/eluser]
file Controller2.php

Include the Controller1.php file before the class declaration
#6

[eluser]Agustín Villalba[/eluser]
Thank you tomcode, I know that include solves the problem, but I'm looking for an "elegant" solution if exists.

n0xie, it seems a good solution, but... how I call that function from my Controller2?? Thank you very much everyone!
#7

[eluser]Phil Sturgeon[/eluser]
[quote author="Agustín Villalba" date="1263921899"]Thank you tomcode, I know that include solves the problem, but I'm looking for an "elegant" solution if exists.

n0xie, it seems a good solution, but... how I call that function from my Controller2?? Thank you very much everyone![/quote]

Instead of:

Code:
class Something extends Controller

do:

Code:
class Something extends Controller1
#8

[eluser]Agustín Villalba[/eluser]
Hi Phil! That's what I do and that is what raise me the error:
Fatal error: Class ‘Controller1’ not found in C:\xampplite\htdocs\project\system\application\controllers\Controller2.php on line XX

Controller1.php

class Controller1 extends Controller {

function Controller1(){

parent::Controller();

}

... }


Controller2.php

class Controller2 extends Controller1 {

function Controller2(){parent::Controller1();

}

I do it just like you suggest me, but it doesn´t work!!
#9

[eluser]n0xie[/eluser]
[quote author="Agustín Villalba" date="1263923164"]
I do it just like you suggest me, but it doesn´t work!![/quote]

Try this:

Code:
function __autoload($class)
{
    if (file_exists(APPPATH."controllers/".strtolower($class).EXT))
    {
        require_once(APPPATH.'controllers/'.strtolower($class).EXT);
    }
}
#10

[eluser]Agustín Villalba[/eluser]
n0xie, I know how to define that function in my config/config.php, but how I use that function in my controller2?? Thank you very much!




Theme © iAndrew 2016 - Forum software by © MyBB