I'm building a site with PHP and a MySQL backend. But, I am currently not using any type of framework. Should I be using Model View Controller?

  • 3
    You don't necessarily need to use a framework to use the MVC design pattern. You can structure your own code to use MVC. – Amy Anuszewski May 28 '11 at 14:10
  • 5
    Are you asking if you should be using a framework or if you should be using the MVC approach in general? – Adam Lear May 28 '11 at 16:34

My rule of thumb:

any page that hits a database or requires any form of user input will be easier to manage with an MVC structure.

You don't necessarily need to go with a whole framework, if the site is fairly simple, you can use a simple Page Controller class for each page that requires it (see above). This isn't a scalable solution mind you - so bear in mind the long term aims of the project.

Here's a rough sketch of a (quickly hacked together) PageController setup:


include 'Controller.php';
include 'Db.php';//db connection
include 'View.php';
$Controller = new MyController(new Db(), new View());

class Controller($db){

    /* ensure all collaborators are provided */
    public function __construct(Db $db, View $view){
         $this->db = $db;
         $this->view = $view;

    /* load the appropriate model data */
    public function route($_GET){
        //load the right model data and template
            case $_GET['articles'] === 'cats':
                $this->vars = $this->db->get('cats');
                $this->template = 'cats.php';
            case $_GET['articles'] === 'dogs':
                $this->vars = $this->db->get('dogs');
                $this->template = 'dogs.php';
             $this->vars = array();


    /* render an html string */
    public function render(){
        echo $this->view->render($this->template,$this->vars);


class View.php
     /* return a string of html */
     public function render($template,$vars){
            // this will work - but you could easily swap out this hack for 
            // a more fully featured View class
            $this->vars = $vars;
            include $template;
            $html = ob_get_clean();
            return $html;


template cats.php
$html = '';
$row_template = '%name%,%breed%,%color%';
foreach($this->vars as $row){
    $html .= str_replace(
echo $html;

I haven't bothered writing a db class... you could just use PDO
| improve this answer | |

I would say that it would be good if there is a chance that the site may grow beyond a non-trivial size. The main reason is you gain better ability to change and maintain the site in the future and maintenance is most of the work on most projects. It gives you a nice separation of concerns, site organization, and helps avoid repetitive and messy code.

MVC is a pattern that is well-known and accepted for websites and that will help if you bring other people onto the project. To that end you would probably want to pick an established framework to start.

| improve this answer | |

You didn't provide any details that might help answering the question, but my default recommendation in such case is "yes, use a MVC framework". Go with a custom solution only when you are really sure that you need it.

| improve this answer | |

As an architecture, MVC focuses on splitting your project/webpage into multiple parts. This can make your life easier when you'll have to change something in the code or the user interface.

Rule of thumb, if you expect changes in specifications about your project especially when those changes affect the entire code, then go with the architecture that forces you to break up the code into tiny lego pieces.

| improve this answer | |

Not at this point. Wait until your site gets bigger and messier. You'll ask yourself - what can I do to make things less messy? You'll read an article on MVC and you will love it. You won't be questioning whether to use it or not anymore. You will know. That would be the moment to start using it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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