2

Every page has dynamic sidebar (column) such as 10 recent articles. It show list in title.

I have to repeat same block of code in every method (action) in the controllers files.

Eg:

<?php
class Blog extends CI_Controller {
    function index()
    {
        // Sidebar code block
            //some code for index
    }
}

class Signup extends CI_Controller {
    function index()
    {
        // Sidebar code block
            //some code for index
    }

    function login()
    {
        // Sidebar code block
            //some code for login
    }
}
?>

In the view folder. I have a sidebar file

There must be a way to void repeating.

2 Answers 2

3

Maybe create a base class and put your function inside it?

<?php
class BaseClass extends CI_Controller {
    function index()
    {
        // Sidebar code block
            //some code for index
    }

}

class Blog extends BaseClass { // Extend your classes from the base class
}

class Signup extends BaseClass {
    function login()
    {
        // Sidebar code block
            //some code for login
    }
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

where is BaseClass php file location?
It's just the custom class I created for this example. You can put it wherever you want.
0

what if you declare it in the constructor , or in one of your config files? http://codeigniter.com/user_guide/libraries/config.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.