CodeIgniter Forums
Extending Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Extending Controller (/thread-7041.html)



Extending Controller - El Forum - 03-21-2008

[eluser]Kemik[/eluser]
Hello,

If I extend the controller, e.g. class Public_Controller extends Controller and then class User_Controller extends Public_Controller.

E.g.
Code:
class Public_Controller extends Controller {

    function Public_Controller() {
        parent::Controller();
        
        $row = 1
        // retrieve all descendants of $root node
        $data['query0'] = $this->controller_model->get_descendants($row);
        
        $this->load->vars($data);
    }
}
class User_Controller extends Public_Controller {

    function User_Controller() {
        parent::Controller();
        
        if (!logged_in()) {
            redirect('user/login');
            return;
        }
    }
}

Will I be able to use $query0 from the Public_Controller in a controller whos parent is User_Controller? I've tried it atm but it just says Fatal error: Call to a member function on a non-object on the line that calls $query0.

Is there a way to fix this or do I need to put the query in each extension?


Extending Controller - El Forum - 03-21-2008

[eluser]wiredesignz[/eluser]
Make $query0 a class var of the parent class, its then available in extended classes as $this->query0; or better yet make the $data array a class var.


Extending Controller - El Forum - 03-21-2008

[eluser]Derek Jones[/eluser]
Code:
function User_Controller() {
        parent::Controller();

You'll want to call your extended class's constructor, not the original Controller() constructor, or your extended class's constructor will never execute e.g.:

Code:
function User_Controller() {
        parent:: Public_Controller();



Extending Controller - El Forum - 03-21-2008

[eluser]wiredesignz[/eluser]
I never noticed that!!, good call Derek.


Extending Controller - El Forum - 03-31-2009

[eluser]dieter[/eluser]
I have a similar issue:

Situation:
Code:
MY_Controller extends Controller () { ... }

Code:
class Controller_A extends MY_Controller
{
    function Controller_A()
    {
        parent::MY_Controller();
    }
}

Works fine

Code:
class Controller_B extends Controller_A
{
    function Controller_B()
    {
        parent::Controller_A();
    }
}

Doesn't work:
Fatal error: Class 'Controller_A' not found in (...)

Any ideas or suggestions to solve this?

Thanks,
Dieter


Extending Controller - El Forum - 03-31-2009

[eluser]louis w[/eluser]
[quote author="dieter" date="1238508108"]I have a similar issue:

Doesn't work:
Fatal error: Class 'Controller_A' not found in (...)

Any ideas or suggestions to solve this?

Thanks,
Dieter[/quote]

Do you have an include_once('Controller_A.php'); before you try to extend it?


Extending Controller - El Forum - 05-24-2009

[eluser]Nexus Rex[/eluser]
I also have a similar issue:

File: system/application/libraries/My_controller.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// application/libraries/My_controller.php

class My_controller extends Controller {

  var $partial = 'debug/echo';

  function My_controller() {
    parent::Controller();
    $this->_get_partial();
    $this->_get_model();
  }
  
  function _get_partial() {
    $uri = $this->router->class . '/' . $this->router->method;
    if (is_file(realpath(dirname(__FILE__) . '/../views/' . $uri . EXT))) {
      $this->partial = $this->router->class . '/' . $this->router->method;
    }
  }
  
  function _get_model() {
    $uri = substr($this->router->class, 0, -1);
    if (is_file(realpath(dirname(__FILE__) . '/../models/' . $uri . EXT))) {
      $this->load->model($uri);
    }
  }

}

When I try to create a members controller that extends My_controller I get an error.

FATAL ERROR: Class 'My_controller' not found in /home/latinole/public_html/directory/system/application/controllers/members.php on line 3

I have tried autoloading the library, moving it to /system/application/controllers/, all to no avail.


Extending Controller - El Forum - 05-24-2009

[eluser]Dam1an[/eluser]
The file should be called MY_Controller.php, although this is only an issue if you're using Linux as its case sensitive


Extending Controller - El Forum - 05-24-2009

[eluser]Nexus Rex[/eluser]
Simple mistake - thank you!