Welcome Guest, Not a member yet? Register   Sign In
controller inherit syntax?
#1

[eluser]überfuzz[/eluser]
I'm setting up a small website, 4 pages. I figured that one controller would be enough. Is it possible to make the classes inherit stuff. Example, see question in the code:

Code:
class Hem extends Controller
{

    function Hem()
    {
        parent::Controller();
        $this->load->model('systemdata');


        $this->data['css'] = $this->systemdata->get_css_filespec();
        $this->data['title'] = $this->systemdata->get_title();
        $this->data['base_url'] = $this->systemdata->get_base_url();
        $this->data['meta'] = $this->systemdata->get_meta();
        $this->data['link_tag'] = $this->systemdata->get_link_tag();
        $this->data['page'] = ucfirst($this->uri->segment(1));

    }
    
    function index()
    {
        $this->load->view('view_hem', $this->data); //separate head, foot, menu from main-view.
    }

}

class Band extends Controller
{

    function Band()  //Is it possible to inherit this from the class: Hem? How would the syntax be if it's possible?
    {
        parent::Controller();
        $this->load->model('systemdata');



        $this->data['css'] = $this->systemdata->get_css_filespec();
        $this->data['title'] = $this->systemdata->get_title();
        $this->data['base_url'] = $this->systemdata->get_base_url();
        $this->data['meta'] = $this->systemdata->get_meta();
        $this->data['link_tag'] = $this->systemdata->get_link_tag();
        $this->data['page'] = ucfirst($this->uri->segment(1));


    }
    
    function index()
    {
        $this->load->view('view_band', $this->data);
    }


}
#2

[eluser]Dam1an[/eluser]
The easiest way to do it would be create a master controller (MY_Controller.php in the libraries folder) and extend that, you can then put in all common contructor code in there.
You would then have to have MY_Controller extends the normal controller so you still have all the CI functionality
#3

[eluser]überfuzz[/eluser]
Hmm, I'm not sure where I can read about it... If I'm guessing, would it be something like this..?
MY_controller.php in libraries
Code:
class MY_Controller extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('systemdata');


        $this->data['css'] = $this->systemdata->get_css_filespec();
        $this->data['title'] = $this->systemdata->get_title();
        $this->data['base_url'] = $this->systemdata->get_base_url();
        $this->data['meta'] = $this->systemdata->get_meta();
        $this->data['link_tag'] = $this->systemdata->get_link_tag();
        $this->data['page'] = ucfirst($this->uri->segment(1));
    }
}

normal_controller.php in the directory controllers
Code:
class Normal_controller extends MY_Controller
{
//Edit start
    function Normal_controller()
    {
        parent::MY_Controller();
    }
//Edit ends
    function index()
    {
        $this->load->view('view_band', $this->data);
    }
}

Did some edit.
#4

[eluser]Dam1an[/eluser]
Yeah, spot on
If you need a constructor in the other controllers (for any page specific stuff), remember to call the parent as well
#5

[eluser]überfuzz[/eluser]
Thanx!!!

I just found a page in the users guide that explains how it's done. Even the part I didn't know; //???
Creating Core System Classes
#6

[eluser]überfuzz[/eluser]
I'm not getting it right, I thought I had the method. When I try to use the MY-controller I get this error message:
Quote:Fatal error: Call to undefined method MY_Controller::my_controller() in /home/minister/public_html/application/controllers/test.php on line 7

test.php in controllers folder
Code:
class Test extends MY_Controller {

    function Test()
    {
        parent::MY_Controller();
    }

    function index()
    {
        $this->load->view('view_hem', $this->data);
    }
}

MY_Controller.php
Code:
class MY_Controller extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('systemdata');


        $this->data['css'] = $this->systemdata->get_css_filespec();
        $this->data['title'] = $this->systemdata->get_title();
        $this->data['base_url'] = $this->systemdata->get_base_url();
        $this->data['meta'] = $this->systemdata->get_meta();
        $this->data['link_tag'] = $this->systemdata->get_link_tag();
        $this->data['page'] = ucfirst($this->uri->segment(1));
    }
}
#7

[eluser]Dam1an[/eluser]
Its because in MY_Controller you use the __construct notation, and in Test, you call the parent using MY_Controller()

You need to either use __construct or the class name in both cases
#8

[eluser]überfuzz[/eluser]
:red:
My nooby eyes didn't see that...
#9

[eluser]Dam1an[/eluser]
Test
Code:
class Test extends MY_Controller {

    function Test()
    {
        parent::MY_Controller();
    }

    function index()
    {
        $this->load->view('view_hem', $this->data);
    }
}

MY_Controller
Code:
class MY_Controller extends Controller
{
    public function __construct()
    {
        parent::Controller();
        $this->load->model('systemdata');


        $this->data['css'] = $this->systemdata->get_css_filespec();
        $this->data['title'] = $this->systemdata->get_title();
        $this->data['base_url'] = $this->systemdata->get_base_url();
        $this->data['meta'] = $this->systemdata->get_meta();
        $this->data['link_tag'] = $this->systemdata->get_link_tag();
        $this->data['page'] = ucfirst($this->uri->segment(1));
    }
}

That should fix it
#10

[eluser]überfuzz[/eluser]
Oh I got it running with this:
Code:
class Test extends MY_Controller {

    function Test()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->view('view_hem', $this->data);
    }
}

Code:
class MY_Controller extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('systemdata');


        $this->data['css'] = $this->systemdata->get_css_filespec();
        $this->data['title'] = $this->systemdata->get_title();
        $this->data['base_url'] = $this->systemdata->get_base_url();
        $this->data['meta'] = $this->systemdata->get_meta();
        $this->data['link_tag'] = $this->systemdata->get_link_tag();
        $this->data['page'] = ucfirst($this->uri->segment(1));
    }
}

Seems I'm getting all the data through to the view file as well.




Theme © iAndrew 2016 - Forum software by © MyBB