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:
Code:
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'] = "[email protected]";
return $data;
}
}
In my Posts Controller, I have tried to flow the DRY principle this way:
Code:
class Posts extends CI_Controller {
public function __construct()
{
parent::__construct();
// Load static data
$this->load->model('Static_model');
$data = $this->Static_model->get_static_data();
// Load Header
$this->load->view('partials/header', $data);
}
public function index()
{
$this->load->model('Posts_model');
$data['posts'] = $this->Posts_model->get_posts();
$this->load->view('posts', $data);
$this->load->view('partials/footer');
}
public function post($id)
{
$this->load->model('Posts_model');
$data['post'] = $this->Posts_model->get_post($id);
if (!empty($data['post'])) {
// Overwrite the default tagline with the post title
$data['tagline'] = $data['post']->title;
} else {
$data['tagline'] = "Page not found";
show_404();
}
$this->load->view('post', $data);
$this->load->view('partials/footer');
}
}
See details of how I came about writing the code above in this topic on stackoverflow.com
The problem with the above code is that the line
Code:
$data['tagline'] = $data['post']->title;
does no longer overwrite the static tagline
Code:
$data['tagline'] = "A simple blog application made with Codeigniter 3";
with the post title. It did overwrite it when the controller look like this:
class Posts extends CI_Controller {
Code:
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');
}
}
Bot this old version of it is goes against the DRY principle.
How can I do the desired overwrite without breaking the "DRY commandment"?