George Kastrinis
George Kastrinis

Reputation: 5182

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.

Upvotes: 18

Views: 21623

Answers (6)

magallanes
magallanes

Reputation: 6854

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.

Upvotes: 14

rlib
rlib

Reputation: 7867

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

Upvotes: 3

shikhar
shikhar

Reputation: 2469

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

Upvotes: 7

anami
anami

Reputation: 152

The comment by Kheldar makes perfect sense. The tutorial actually covers making a micro framework using the MVC pattern.

Although you'd need to add to it - in terms of adding custom routes and some sort of routing engine but apart from that it is a very good baby step into developing your own MVC framework..

Upvotes: 0

TJHeuvel
TJHeuvel

Reputation: 12608

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.

Upvotes: 9

Chris Laplante
Chris Laplante

Reputation: 29658

Although your question is a little too broad, I think I can provide some help.

The number one question I had when starting out with frameworks was: Which framework, if any, should I use? In your case, I would not try to build a MVC website without a premade framework. Most of the architecture you will end up writing has already been done dozens of times over.

Things like DB abstraction, login systems, etc. are boring to write. Might as well use a framework that already has these things.

I would highly suggest this book: http://www.amazon.com/Building-Applications-Symfony-CakePHP-Framework/dp/0470887346/ref=sr_1_2?ie=UTF8&qid=1315227178&sr=8-2. It will walk you through building a simple MVC website using three different PHP MVC frameworks. By the end of the book, you will have enough experience with each framework so that you can choose which one you like best.

Alternatively, if you'd like to write your own framework so that you can learn how it's done, I'd recommend TJHeuvel's answer. Another option is to read the source code of an existing framework so you can see how others have implemented it.

Upvotes: 2

Related Questions