11

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?

2
  • 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. May 28, 2011 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, 2011 at 16:34

5 Answers 5

11

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:

index.php
--------------------------------------------------------

include 'Controller.php';
include 'Db.php';//db connection
include 'View.php';
$Controller = new MyController(new Db(), new View());
$Controller->route($_GET);
$Controller->render();


Controller.php
--------------------------------------------------------
class Controller($db){

    /* ensure all collaborators are provided */
    public function __construct(Db $db, View $view){
         $this->db = $db;
         $this->db->connect(array('host','db','user','pass'));
         $this->view = $view;
    }

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

    } 

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

}

View.php
------------------------------------------------------------
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;
            ob_start();
            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(
        array(%name%,%breed%,%color%),
        array($row['name'],$row['breed'],$row['color']),
        $row_template);
    }
echo $html;

Db.php
---------------------------------------------------------------
I haven't bothered writing a db class... you could just use PDO
0
3

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.

0
1

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.

1

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.

1

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.

Your Answer

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

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