Php – redundant implementation in CI controller

codeigniterPHP

I wanna ask you best practices about blog front page.
I wanna build blog application using CodeIgniter framework. I have 2 type of page (front page, and admin page)

Supposed I have several controller in my front page (home, post, page, and link). By default I have include viewer of for all of these controller: header.php, footer.php, sidebar.php.

In the sidebar, I always display categories, recent comment, recent post, links, and archived.So .., In all of my front page controller I must implement select of categories, recent comment, recent post, links, and archived. Supposed I implement in all controller's constructor.

__construct () {
//data['categories'] = CategoryModel->getlist
//data['recent_posts] = PostModel->get_recent_post
//etc

can you suggest me, where I must place this method so I mustn't implement this method in all controller.

Thanks

Best Answer

You can write a base controller which the other ones inherit from

class AppStartup extends Controller {

    function __construct() {
        // whatever you need
    }

}

then

class Home extends AppStartup {

    // ....

}

Also you could start accepting some of the answer given to you, or people won't be so happy to help you.

Related Topic