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

2019-01-13 02:02发布

问题:

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.

回答1:

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


回答2:

A simple exemple implementation of MVC (just to understand the principle)

MODEL: lib/Thing.class.php

class Thing
{
//class code ( CRUD, the application logic ...)
}

VIEW: theme/page_thing.php

<?php require("header.php");?>
//HTML CODE with some echo to show variables and loops to read arrays
<?php require("footer.php");?>

CONTROLLER: application/thing.php

require_once("lib/Thing.class.php");
/*
Some controls between the Model and the View ( if/else ...)
*/
include("theme/page_thing.php");


回答3:

I, too, wrote an homegrown MVC framework in PHP. Its pretty simple, especially when you remove any "ActiveRecord" functionality from your frame work. Some things that I considered:

How are you going to map URLs to controllers?

Instead of doing things by convention (/foo maps to FooController), I did everything via configuration. That is, I have a master routes.php file wherein I list every possible URL that my application will accept. So its filled with things like:

Router::add( '/foo/:Param1/:Param2', 
             array( 'Controller' => 'MyController', 
                    'Action' => 'my_method', 
                    'Method' => 'GET', 
                    'Parameters' => array( 'Param1' => '\d+',
                                           'Param2' => '\S+' ) );

In this case we match urls like /foo/123/abc. When the URL is matched, it is dispatched as MyController::my_method( array( 'Param1' => '123', 'Param2' => 'abc' ) );.

How are you going to generate views?

There are lots of templating systems out there. But really, PHP is already a perfect templating system. In my framework, I just created a function template() in the top-level Controller class. And it all boils down to performing an include $Template. Again, in my framework, there is no convention. Each controller is responsible for instantiating the appropriate template, and for understanding if the request is expecting HTML, XML, or JSON as a response.

Can you use an existing framework?

A lot of my code was inspired by Cake, the well-known PHP MVC framework. I'd definitely take a peek at it before you proceed to far. If you're going to roll your own, at least start by understanding how all of the popular ones work. In the end, the peculiar requirements of my application made me go down the road of build my own, but there was a lot to be learned from all the frameworks already out there. Take a long look around, and you may find something that works for you. At the very least, you may figure out exactly what you need out of your framework.



回答4:

I personally use my own framework consisting of :
1.Mysql Interface
2.Template System (yes home brewed not smarty)
3.Config Class (mysql details,debug, and anything else that the script might need)
4.Simple Form Creating class.
5.a Request Class (all useful details from $_SERVER in a more readable format ex: $this->Request->ip, $this->Request->url,$this->Request->time)
6. Anti-hacking (Ip blacklist, keywords from public sec. scanners etc.)
And I just call it framework :)



回答5:

if you're just going to "recyle" the wheel, you can take a look at the source code of "popular" frameworks. if you want to "reinvent" the wheel, i suggest you look elsewhere. examine domain-specific languages (DSL).