Welcome Guest, Not a member yet? Register   Sign In
Subclassing Controller
#1

[eluser]stoefln[/eluser]
I have a class "SuperController" which extends COntroller.
I wand to use this class for all my controllers. is it possible to include the supercontroller anywhere so i dont have to use require_once in each controller?
#2

[eluser]Seppo[/eluser]
Name it MY_Controller so CI will load it when it loads Controller class
#3

[eluser]wiredesignz[/eluser]
MY_Controller

Edit:
Damn.. too slow again. Good work Seppo :lol:
#4

[eluser]stoefln[/eluser]
yeah, i love CI!
#5

[eluser]stoefln[/eluser]
when i supply my subclasses with methods which i have defined in MY_Controller, e.g. "render"- is there a possibility to ensure that these methods can't be called from the frontend (browser)?
#6

[eluser]wiredesignz[/eluser]
Underscore prefix the method definition, _myMethod(), underscore prefixed methods are not available to URL
#7

[eluser]stoefln[/eluser]
[quote author="wiredesignz" date="1213810063"]Underscore prefix the method definition, _myMethod(), underscore prefixed methods are not available to URL[/quote]

true. but also not callable in my subcontroller:

$this->_render(...);

results in

Fatal error: Call to undefined method Types::_render() in D:\htdocs\codeigniter\system\application\controllers\types.php on line 46
#8

[eluser]Pascal Kriete[/eluser]
Probably stating the obvious - the controller has to a) extend MY_Controller and b) both the method and the call need to be changed.

Other than that, I have no idea what could be going wrong.
#9

[eluser]stoefln[/eluser]
[quote author="inparo" date="1213811508"]Probably stating the obvious - the controller has to a) extend MY_Controller and b) both the method and the call need to be changed.

Other than that, I have no idea what could be going wrong.[/quote]

ups. checked it again and found the problem: had 2 files and made the edits on the old, unused one. Tongue

thanks for the help!
#10

[eluser]taewoo[/eluser]
Is there a way to extend the "MY_Controller"?

Meaning this MY_Controller is an abstract class that cannot be extended... then two sub-classes of MY_Controller (say Uno_Controller and Dos_Controller) that actually does "stuff":

Example, in application/library/

Code:
abstract class MY_Controller extends Controller
{
   protected $var_1, $var_2;
   function __construct($var_1, $var_2)
   {
         $this->var_1 = $var_1;
         $this->var_2 = $var_2;

         $this->_authenticate();
   }

   abstract public function _authenticate();
}

then ALSO in library, there are two that extend this MY_Controller:

Code:
class Uno extends MY_Controller
{
   function __construct()
   {
         parent::__construct("Hello", "World");
   }

   public function _authenticate()
   {
         print_r($this->var_1);
         print_r($this->var_2);

   }
}

class Dos extends MY_Controller
{
   function __construct()
   {
         parent::__construct("Fart", "Burp");
   }

   public function _authenticate()
   {
         var_dump($this->var_1);
         var_dump($this->var_2);

   }
}

Then in the actual application/controller/XYZ.php, you extend either Dos or Uno.

I can't seem to get this to work.




Theme © iAndrew 2016 - Forum software by © MyBB