How to implement mvc in core php

how is mvc architecture used in php without any frameworks?

+7
php
source share
8 answers

I think that, as a rule, using one of the common frameworks is probably the way to go. The reason is that many good developers for a long time wrote, fixed bugs, tuned and polished to create something solid to base your site on. It's best to find what you like, get to know it and stick to it (unless you find a reason not to do it). When I work with PHP, my choice is usually the Zend Framework , but there are CodeIgniter , Symfony , CakePHP and a group of others .

If you still want to use the MVC template without an existing structure, you either have the choice to compose yourself or simply logically separate each problem from each other - this is the basic principle of MVC, the framework just helps you achieve this.

Rasmus Lerdorf wrote about his minimal approach to the MVC pattern in PHP in 2006. Perhaps worth a read. You may also be interested in a mini-structure like F3 :: PHP (only PHP 5.3+) - it looks pretty promising.

+13
source share

Simple PHP MVC approach

Thousands of words do not compete with pure example, so here is a simple example:

Imagine that you want to display a page describing a “car” (including a “car identifier”) with an imaginary car seller: http://example.com/car.php?id=42 )

Basically, you can structure your code using a hierarchy, for example:

Configuration directory (this is not part of the MVC architectural pattern):

+ config/ - database.php <?php return new PDO("mysql:host=localhost;dbname=database", "user", "password"); ?> 

Folder for your document root (scripts acting as controllers ):

 + htdocs/ - car.php <?php require_once 'CarService.php'; $carService = new CarService(require 'config/database.php'); $car = $carService->getInformationById($_GET['id']); require 'car.tpl'; ?> 

Folder encapsulating the whole Model (tooltip: "Thin controllers, Fat model"):

 + model/ - CarService.php <?php class CarService { protected $database; public function __construct(PDO $database) { $this->database = $database; } public function getInformationById($id) { return $this->database->query( "SELECT model, year, price " . "FROM car " . "WHERE id = " . (int) $id )->fetch(); } } ?> 

The last folder containing all of your Views (/ templates):

 + views/ - car.tpl <html> <head> <title>Car - <?php $GLOBALS['car']['model'] ?></title> </head> <body id="car"> <h1><?php $GLOBALS['car']['model'] ?></h1> Year: <?php $GLOBALS['car']['year'] ?> Price: <?php $GLOBALS['car']['price'] ?> </table> </body> </html> 

What is it.

For completeness

You may notice the use of $ GLOBALS in templates, it can be a convenient coding standard for denoting local template variables from those that you get from the "controller".

In order for the above code to work, you will need PHP to configure:

 include_path="/the/path/to/model:/the/path/to/views" 

Go further

Good URLs

you may need good urls if you are using apache you can achieve this with

 RewriteEngine On RewriteRule ^/car/([0-9]*)$ /car.php?id=$1 [L] 

This allows you to record URLs such as http://example.com/car/42 that will be internally converted to http://example.com/car.php?id=42

Reusable patterns

Being PHP files, nothing prevents you from creating headers.tpl, footers.tpl, menu.tpl, ... which you can reuse with include () / require () to avoid duplication of HTML.

Conclusion

This is very similar to what Rasmus Lerdorf said: http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html .

Remember that MVC remains an (n) (architectural) pattern . Software templates are reusable principles for solving common problems, if they are reused by code , they would be called "libraries" instead.

Frames such as Zend Framework, Symfony, CakePHP, etc., offer a framework for adopting the MVC approach, but cannot enforce it, since MVC is an architectural pattern that needs to be learned and understood whether you are using an existing framework or not.

+11
source share

You can check out the PHP MVC Tutorial to learn how to use a simple MVC pattern from scratch, rather than in an existing structure.

+3
source share

This is not true. Core PHP is "the beginning of a global namespace statement and expression-oriented language." You need additional code (and an additional Rewriter URL) to implement any MVC architecture. This extra code is your structure.

+1
source share
+1
source share

To achieve the MVC template, you just need to separate the data storage code (“model”, mainly database material), the main application logic (“controller”) and your presentation in the outside world (“view”, for example, HTML pages or RSS channels).

IF you just don't mix these three parts in your code, you already have a truly basic MVC architecture. Just create separate classes for the model, view and controller layers, come up with a well-structured way of how they talk to each other, and then stick to it!

For the convenience of code maintenance, you ALWAYS try to work this way.

+1
source share

Try integrating Pear DB Layer, Smarty, PHP GACL into your core code to achieve MVC architecture.

0
source share

By creating your own MVC framework that creates MVC patterns and OOP principles :)

  • You must have Front Controller, so each HTTP request goes through a single file, index.php, app.php or whatever. Thus, you can configure the application in one place.

  • From there you will need a routing mechanism that will analyze the HTTP request, the current URL, the HTTP header / method and based on the fact that you will call the appropriate controller / controller action controller.

  • From Controller, you can access your Models that will be engaged in heavy lifting, deal with the database and domain / business logic, etc. And from Controller you can display Views.

So you need at least Front Controller, Router / Dispatcher, Controller, Models and Views for a simple MVC architecture.

You would do it like other MVC web frameworks, with slight variations depending on your preference.

Take a look at some simple structures like Codeigniter and read their source code to get an idea of ​​how they make MVC.

And have fun creating your MVC! In the end, it's all fun: D

0
source share

All Articles