Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement MVC from scratch in PHP? [closed]

I would like to implement MVC from scratch in PHP because I want full control of my own code and no extra bagage from existing frameworks. Anyone who has any advice?


Yes, I've seen Lerdorfs article and it seems that it ain't so much code after all. Actually I would more like to have a controller-view solution for structuring my application. I'll stick to my own homemade PDO data-access classes.

like image 854
poo Avatar asked Feb 25 '10 09:02

poo


People also ask

How MVC is implemented in core PHP?

To achieve a MVC pattern you just have to separate your data persistence code ("model", mostly database stuff), the main application logic ("controller") and your presentation to the outside world ("view", like HTML pages or RSS feeds).

Can PHP be used for MVC?

PHP has a number of open source mature and tested MVC frameworks. A good development approach separates the data from the presentation and encourages the use of single entry point into an application.

What is controller in PHP MVC?

Controllers purpose in MVC pattern is to alter the state of model layer. It's done by controller receiving user input (preferable - abstracted as some thing like Request instance) and then, based on parameters that are extracted from the user input, passes the values to appropriate parts of model layer.


1 Answers

Your question somewhat smells like Not-Invented-Here-Syndrome. In this case, my advice would be to live with the extra baggage of existing frameworks when you can be sure they are thoroughly tested and supported. Don't reinvent the wheel.

On the other hand, the above argumentation would prevent new frameworks to be written. And writing one from scratch is a good coding exercise to learn and understand the MVC pattern.

So if you are really determined to do it, my suggestion is to learn what each part of MVC is, does and how they interact. You will inevitably come across the FrontController pattern as well, so you will want to learn about this one too.

Note that you are not the only person wanting to do this:

  • http://www.google.de/search?q=front+controller+php
  • http://www.google.de/search?q=build+your+own+mvc+php

And there is also this interesting article by Rasmus Lerdorf

  • http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html
like image 197
Gordon Avatar answered Sep 17 '22 19:09

Gordon