NDBoost
NDBoost

Reputation: 10634

CodeIgniter + App + App Updates with view customizations

I have an app that uses HMVC and codeigniter. The entire app is in a git repo called MapIt-USA. I just ran into a scenario where i deployed this app to a client xyz and they wanted me to customize the front-end view layout. I made the modifications however, when i make backend controller, library, model patches/updates and i push these updates to origin and pull down from origin on the server I need a way to avoid overwriting those changes in the views.

Any ideas?

Upvotes: 0

Views: 77

Answers (1)

No Results Found
No Results Found

Reputation: 102745

Basic idea for overloading views (or any file, really):

  • Store custom views somewhere where the main application updates won't overwrite them. Give these the same name as the default ones.
  • Modify your template loader to check for the custom file first, if it doesn't exist then use the default one. If you don't have a custom solution to loading templates, now's the time to write one or extend the CI Loader class to accommodate this change.

There will probably come a time when you have the need to overload or extend more than just views: maybe config files, helpers, language files, or even controllers. So, you might want to start thinking about how to deal with this in the long run. You can mimic the way CI works, looking in the system/ for files first, while allowing application/ files to extend or overload. Customizations of course would bear the burden of staying compatible.

Short example with views:

class MY_Template_Loader {

    // We'll assume this is in your application/ dir
    private $custom_path = 'custom_views/';

    function load($file = NULL)
    {
        // This is the default view
        $view = $file;

        // Is there a file with the same name in the custom dir?
        // If so, use that instead of the default
        if (is_file(APPPATH.$this->custom_path.$file.'.php'))
        {
            // This is a little bit of a trick
            // Use a relative path from CI's default view dir
            $view = '../'.$this->custom_path.$file;
        }
        get_instance()->load->view($view);
    }

}

Usage in a controller method:

function my_method()
{
    $this->my_template_loader->load('my_method/index');
    // If "APPPATH/custom_views/my_method/index.php" exists it will be loaded
    // Otherwise it will try to load "views/my_method/index.php"
}

How you really do this is up to you, but that's the basic idea.

Upvotes: 1

Related Questions