Welcome Guest, Not a member yet? Register   Sign In
best way to handle inheritance
#1

[eluser]thors1982[/eluser]
So I am having issues on how to handle inheritance in Codeigniter.

I am slightly dumbing this down so its easier to understand... however essentially what I want to do is have a generic "Page" controller then have other controllers inherit off that... such as "Contact Us", "About Us", etc.

The generic page could have functions like "edit", that would ask for title, meta tags, content. But the contact us could request all the above as well as email address, form fields etc.

My problem is I can't seem to inherit off another Controller. I have extended the base controller into a secure and unsecure controller which works fine (and i can use those generic functions) but that file is stored in the core folder.

Any tips on how to get this done? (not necessarily JUST get it done) But the best codeigniter way to get it done :-) I am away I could probably drop ALL those classes and functions in one file and it would work fine.... but that does not sound like a very good solution.

Thanks for the help!
#2

[eluser]n0xie[/eluser]
Use an autoloader.
#3

[eluser]waynhall[/eluser]
Create base controller:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class BASE_Controller extends CI_Controller {
    
    function __construct() {
        
        parent::__construct();
        
        $student;
        $this->load_student();
    }

    protected function isLoggedIn(){
        return $this->session->userdata('logged_in');
    }
    
    protected function login($email){
        $newdata = array(
                'email'     => $email,
                'logged_in' => TRUE
        );

        $this->session->set_userdata($newdata);
    }
    
    public function logout() {      // This could be accessed from url: http://mysite.dev/student/logout
        $this->session->sess_destroy();
        redirect('/', 'location');
    }
    
    protected function load_student() {
        if($this->isLoggedIn()){
            $this->load->model('students');
            $email = $this->session->userdata('email');
            $this->student = $this->students->get_student($email);
        }
    }
}

Public methods will be accessible from the url, but protected functions will not.

Then, include the file and inherit BASE_Controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include_once('BASE_Controller.php');

class Student extends BASE_Controller {


}
#4

[eluser]waynhall[/eluser]
[quote author="thors1982" date="1311891393"]however essentially what I want to do is have a generic "Page" controller then have other controllers inherit off that... such as "Contact Us", "About Us", etc.

The generic page could have functions like "edit", that would ask for title, meta tags, content. But the contact us could request all the above as well as email address, form fields etc.[/quote]

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends CI_Controller {
    
    function __construct() {
        
        parent::__construct();
        
        $page;
    }

    protected function edit($data){
        
        // code to edit a page
        
    }
    

}

then include and inherit:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include_once('page.php');

class Contact_us extends Page {
    
    function __construct() {
        parent::__construct();
    }
    
    public function index()    {
        
            $data = array(
                'title' => '',
                'meta_tags' => '',
                'content'   => ''
            );
            
            $this->edit($data);
       }
        
}
#5

[eluser]thors1982[/eluser]
Thanks waynhall

That works perfectly. I have no idea why I didn't think of it before (guess I am just so used to codeigniter including files for me now :-) That escaped me.

I saw the autload method but that seems inefficient to auto load all controllers when I just want to extend one.

Thanks for the help everyone!




Theme © iAndrew 2016 - Forum software by © MyBB