Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

One of our customers asked us to add some dynamic functionality to an existent website, made of several static HTML pages.

We normally work with an MVC framework (mostly CodeIgniter), but in this case moving everything to a framework would require too much time.

Since it is not a big project, not having the full functionality of a framework is not a problem. But the question is how to keep code clean.

The solution I came up with is to divide code in libraries (the application's API) and models. So inside HTML there will only be API calls, and readability will not be sacrificed.

I implemented this with a sort of static Registry (sorry if I'm wrong, I am not a design pattern expert):

<?php
class Custom_framework {

    //Global database instance
    private static $db;

    //Registered models
    private static $models = array();

    //Registered libraries
    private static $libraries = array();

    //Returns a database class instance
    static public function get_db(){

        if(isset(self::$db)){
            //If instance exists, returns it
            return self::$db;
        } else {
            //If instance doesn't exists, creates it
            self::$db = new DB;
            return self::$db;
        }
    }

    //Returns a model instance
    static public function get_model($model_name){

        if(isset(self::$models[$model_name])){
            //If instance exists, returns it
            return self::$models[$model_name];
        } else {
            //If instance doesn't exists, creates it
            if(is_file(ROOT_DIR . 'application/models/' . $model_name . '.php')){
                include_once ROOT_DIR . 'application/models/' . $model_name . '.php';
                self::$models[$model_name] = new $model_name;
                return self::$models[$model_name];
            } else {
                return FALSE;
            }
        }
    }

    //Returns a library instance
    static public function get_library($library_name){

        if(isset(self::$libraries[$library_name])){
            //If instance exists, returns it
            return self::$libraries[$library_name];
        } else {
            //If instance doesn't exists, creates it
            if(is_file(ROOT_DIR . 'application/libraries/' . $library_name . '.php')){
                include_once ROOT_DIR . 'application/libraries/' . $library_name . '.php';
                self::$libraries[$library_name] = new $library_name;
                return self::$libraries[$library_name];
            } else {
                return FALSE;
            }

        }
    }

}

Inside HTML, API methods are accessed like this:

<?php echo Custom_framework::get_library('My_library')->my_method(); ?>

It looks to me as a practical solution. But I wonder what its drawbacks are, and what the possible alternatives.

share|improve this question
    
Knowing the type of "dynamic functionality" that needs to be added would help. Just going by what you posted, however, I would say that the use case calls for Wordpress. –  user16764 Jul 1 '12 at 16:50
    
A user database and some content only visible to premium users. There won't be a complete authentication system, since users will be added and managed by an administrator. Anyway my question is more about a general solution than this specific case. –  lortabac Jul 1 '12 at 16:59
add comment

1 Answer

Your method is overcomplicated if you are only going to implement authentication/authorization to access some static pages;

<?php include "check_access.php" ?>

at the top of the protected pages would be all you need, plus a login form and form handler.

You might want to use PHP simply as an SSI mechanism to remove HTML duplication boilerplate (i.e. headers, footers, etc.)- includes might be all you need, although you might want to introduce a better templating mechanism.

The big decision is, are you going to adapt the existing parts of the website to be dynamic? How much more will you need to implement after you deliver this functionality?

If you're not going to do much work on the existing site, and it is big, it might makes sense to do everything new using your preferred framework/methodology. OTOH, framework-less PHP sucks, so redoing everything with your framework of choice might make sense.

In any case, unless what you need to implement is very simple, I would suggest doing it with a framework, even if you don't touch the old pages.

share|improve this answer
    
Yes, the customer might ask us to adapt the existing parts to be dynamic, so maybe includes are not the right choice. The problem is that moving all pages to a framework would require too much work, and adding plain PHP to HTML is chaos. So I thought my solution could be a compromise, because it somehow simulates framework behaviour but doesn't require controllers nor URL rewriting. –  lortabac Jul 4 '12 at 17:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.