I had a very short look at lightblog/application/controllers/Pages.php
and I noticed that these two values were not used in the page()
method:
$data['pages']
$data['posts']
Then I realized that you hand over data
to other objects and they might be using these two values. Who knows? This makes your code very hard to understand and to debug.
Basically you're treating the $data
array as a sort of object you hand over to other classes.
But wait, did I say 'object'? But what if it was? Clearly you have a need to keep data accessible, but why use an array that you need to recreate every time? Why not use a class?
Now I don't have any idea how your code is structure, and which class would be the best to use for this. But it would make sense to extend the basic CodeIgniter controller class to hold this data, like so:
class Data_Controller extends CI_Controller
{
private $data;
public function __construct()
{
parent::__construct();
$this->data = $this->Static_model->get_static_data();
$this->data['pages'] = $this->Pages_model->get_pages();
$this->data['categories'] = $this->Categories_model->get_categories();
$this->data['posts'] = $this->Posts_model->sidebar_posts($limit=5, $offset=0);
$this->data['page'] = $this->Pages_model->get_page($page_id);
}
public function getData()
{
return $this->data;
}
}
or something to the same effect and then extend this for your pages class:
class Pages extends Data_Controller {
public function __construct()
{
// currently this whole method doesn't do anything, it can be removed
parent::__construct();
}
public function page($page_id) {
$data = $this->getData();
..........................
}
}
And, of course, those controller classes that don't need all that data can just extend CI_Controller
.
I'm sure there are other ways of doing this. I don't like the fact that now you still collect all that information without actually knowning if it is needed. I would make the retrieval of the data dependent on the fact that it is actually used. In other words use getter methods, not a data array. I'll give an example for 'pages' and 'posts', but you have to create the other ones yourself:
class Data_Controller extends CI_Controller
{
public function getPages()
{
return $this->Pages_model->get_pages();
}
public function getCatagories()
{
return $this->Categories_model->get_categories();
}
}
If you need to buffer the data you can do:
class Data_Controller extends CI_Controller
{
public function getCatagories()
{
static $buffer = NULL;
if (is_null($buffer)) $buffer = $this->Categories_model->get_categories();
return $buffer;
}
}
Keep in mind that this buffer will work for all instances of Data_Controller
. That can be an advantage, or a disadvantage.
OK, that was a bit long. I hope you got some new ideas from my ramblings.
$this->Static_model
why not make a new config for this data, it seems like a better way. Because you can then create a development copy etc. codeigniter.com/user_guide/general/… \$\endgroup\$