问题
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!
回答1:
You can load the model in the constructor:
public function __construct()
{
parent::__construct();
$this->load->model('Static_model');
}
Since the constructor gets called every time the class is instantiated, it will always be available to any methods called later.
Regarding the views, you can use require()
or include()
as usual to load partials from the view file.
回答2:
I have managed to obtain a controller configuration hat is a lot closer to the being optimal then the initial one:
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);
// Overwrite the default tagline with the post title
$data['tagline'] = $data['post']->title;
$this->load->view('post', $data);
$this->load->view('partials/footer');
}
}
The line $this->load->view('partials/footer');
still appears in both functions, but I am closer to respecting the DRY principle.
This configuration of the controller has solved a problem and created another: the line $data['tagline'] = $data['post']->title;
has no effect now.
来源:https://stackoverflow.com/questions/49705564/codeigniter-3-blog-application-how-can-i-avoid-redundancy-of-static-data