loading...
Cover image for A basic implementation of Model-View-Controller pattern in only 20 lines of code based on OOP

A basic implementation of Model-View-Controller pattern in only 20 lines of code based on OOP

adavidoaiei profile image Adavidoaiei Dumitru-Cornel Updated on ・1 min read

I will present in this article a easy way to separate PHP code from HTML code using Model-View-Controller pattern, almost all PHP MVC frameworks implement this, at the beginning of PHP was common to interpolate PHP code with HTML code, the PHP code runs on server and generate HTML code.

index.php


.htacces redirects all requests to index.php.

What is doing the following request: http://localhost/forecast/index   ?
It is searching the class forecast in the controllers folder if exists creates object forecast, after that checks if exists method index and invokes it, this is a basic implementation of routing in a common MVC frameworks.
Root of web application has the following structure:
-folder controllers
-folder models
-foder views
-file .htaccess
-file index.php


/controllers/forecast.php

/views/forecast.html

/models/money_per_month.php

The complete code could you find on GitHub: https://github.com/adavidoaiei/Model-View-Controller-Pattern

Posted on Apr 21 by:

Discussion

markdown guide