ytsejam
ytsejam

Reputation: 3439

Is this a good way for separating admin and public for my application in Codeigniter?

I am creating a website with static pages and admin editable news and content. I want to separate admin and public views. Is this a good way to separate views or Is there a better one you can suggest. I want basically same views but only admin will have a form based text area to change content of a topic.

MY_Controller.php :

class MY_Controller extends CI_Controller {

  protected $data = array();

  function __construct() {
    parent::__construct();
  }
    class Admin_Controller extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        // Check login, load back end dependencies
    }
    function render_page($view) {
    if( ! $this->input->is_ajax_request() )
    {
      $this->load->view('templates/header', $this->data);
    }
    $this->load->view($view, $this->data);

    if( ! $this->input->is_ajax_request() )
    {
     $this->load->view('templates/menu');
     $this->load->view('templates/footer', $this->data);
    }
  }  
}
class Public_Controller extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        // Check login, load back end dependencies
    }


   function render_page($view) {
    if( ! $this->input->is_ajax_request() )
    {
      $this->load->view('templates/header', $this->data);
    }
    $this->load->view($view, $this->data);

    if( ! $this->input->is_ajax_request() )
    {
     $this->load->view('templates/menu');
     $this->load->view('templates/footer', $this->data);
    }
  }  
}
}

Upvotes: 0

Views: 91

Answers (1)

rrrhys
rrrhys

Reputation: 654

If the admin view is common across pages, you could load it as a view at an appropriate spot on your page, eg.

if($is_admin){
$this->load->view('admin_toolbar');
}else{
$this->load->view('generic_toolbar');
}

Or, if the admin view is not common across pages, you could include a flag in the data you send to the page.

(In controller)

$this->data['is_admin'] = true;

(In view)

if($is_admin):
//output admin related html here.
endif

Upvotes: 1

Related Questions