I am working on a blog application in Codeigniter 3.1.8.
I have a model with "static" data like the website's title, the contact email address, etc:
class Static_model extends CI_Model {
public function get_static_data() {
$data['site_title'] = "My Blog";
$data['tagline'] = "A simple blog application made with Codeigniter 3";
$data['company_name'] = "My Company";
$data['company_email'] = "company@domain.com";
return $data;
}
}
In my Posts Controller, which handles both the posts page and the single post page, I was forced to load the Static_model twice:
class Posts extends CI_Controller {
public function index()
{
$this->load->model('Static_model');
$data = $this->Static_model->get_static_data();
$this->load->model('Posts_model');
$data['posts'] = $this->Posts_model->get_posts();
$this->load->view('partials/header', $data);
$this->load->view('posts');
$this->load->view('partials/footer');
}
public function post($id) {
$this->load->model('Static_model');
$data = $this->Static_model->get_static_data();
$this->load->model('Posts_model');
$data['post'] = $this->Posts_model->get_post($id);
// Overwrite the default tagline with the post title
$data['tagline'] = $data['post']->title;
$this->load->view('partials/header', $data);
$this->load->view('post');
$this->load->view('partials/footer');
}
}
As you can see, the header and footer partials ware also loaded redundantly.
Questions:
- How can I load the Static_model only once and so that both the index() and post() methods can use it?
- Also, how can I load the partials only once per controller?
Thanks!