Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to MVC in php without the use of some framework

I have some (basic perhaps) knowledge of the Model-View-Controller pattern and I want to create a site using this. But I find it a bit confusing how to actually implement this. I get stuck in the details.

Say that I have a site where each user keeps some todo lists. How would you approach this? What classes would you create? Which class would output the HTML, which class would server as the controller and how would it communicate with the view to produce the output? etc.

Sorry if it seems silly and I guess it must be somewhat easy but I am stuck.

like image 623
George Kastrinis Avatar asked Sep 05 '11 12:09

George Kastrinis


4 Answers

For the record:

It is not as hard to do a MVC in PHP, its more related with to be disciplined rather to be a difficulty task.

a) Model(s) (optional, you can use an array in PHP)

<?php
     class MyModel() {
     }
?>

b) Route (index.php?)

<?php
include "...";
// here we collects all the information, such post,get and path values
$action=...;
$param=....;
switch($controller) {
      case "my": // www.myweb.com/my/action
      include "controller\MyController.php"; // open the right controller.
      break;
}
?>

c) Controller

<?php
include "model\MyModel.php";
switch($action) {
    case "add":
         // here live the logic, information, call for services and such.
         $model=....;
         // and finally...
         include "view\MyView.php";
         break;
}
?>

d) View

<html>
    <body>
       <?=$model->field;?>
    </body>

<html>

As a note:

a) The view should be clean as possible. Think that the view could be created by a web designer that doesn't care about php.

b) The view is always the last step of the process. The view web always returns nothing.

like image 187
magallanes Avatar answered Nov 10 '22 00:11

magallanes


It is very possible to do this without an existing framework, and just create your own. Its not a very difficult task anyway.

Not being application-specific, your MVC framework would have to do the following:

  1. Redirect all trafic to a central page, so that every request gets handled correctly.
  2. Extract the controller and action from the request url. (for example, a request to http://yoursite.com/Task/Add, you have to translate that to the Add method on the TaskController)
  3. Load the controller class (in our example TaskController). Perhaps using Autoload.
  4. Call the Add method on the Controller
  5. Show the result

There are multiple ways to implement views, you could emulate ASPMVC and have each Controller's action return an ActionResult, which has one method Execute. Then an overload of that, ViewResult would take care of loading the correct view and including it with the proper ModelData.

like image 27
TJHeuvel Avatar answered Nov 10 '22 01:11

TJHeuvel


Here's the precise answer to your question from RASMUS LERDORF himself. Read through.

like image 40
shikhar Avatar answered Nov 10 '22 01:11

shikhar


Read the following intro to Symphony network: http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html

like image 34
rlib Avatar answered Nov 10 '22 00:11

rlib