问题
Update
I have looked into various php frameworks (yii framework looks particularly good/interesting) and they seem to offer some very good features compared to simple template engines (including MVC and other features like database integration). I am certain that some from of separation between code and display is needed. Whether that is just plain php (as a template engine), a template engine (e.g smarty) or a framework probably depends a lot on the application and the company/programmers choice. (and an issue I can continue to research in my own time, and will probably not get definite answers for)
I still have one remaining question. Is it possible to have a multi-tiered setup (as in multiple separate individual servers) where one runs php code ("application logic"), which is outputted in a form such as XML or JSON or any other data interchange format, that then gets sent to the web/HTML server which takes the output from the php code and converts it into HTML so that the total average number of pages served per a second is faster than just using a single tier. (even if that single tier got all the servers from the two separate tiers combined for itself. Also I'm guessing the parsing time for XML (and probably JSON) would decrease it, but a new protocol between the two tiers could be used that is optimized for this purpose.)
I was pondering HTML and code separation (both to implement MVC and allow web designers (as in looks/views) and web developers (as in code/controllers) to work independently) and I thought it should be possible to have application servers that run PHP (Application/Business Logic/Controller) and web servers that take the output from the application servers and insert it into HTML markup (Looks/Views).
In theory it would work a bit like the separation of an application server and a database server, while for a single request it might be slightly slower for that one user due to network overhead you can handle considerably more simultaneous requests with two small servers than one big server. For example the application server could send it's processed (view independent) information to the web server that would then insert that into the HTML (which could be different depending on the client, e.g mobile browsers). It may be possible to cache the HTML in ram but not the dynamic content so that even if the page looks fairly different for every user (e.g Facebook) some speed benefit is still gained compared to a single HTML/PHP combo. It would also, of course, separate the HTML from the application code (PHP).
I looked into this and I came across numerous PHP template engines which would facilitate the separation of HTML from code, however some of them are considerably slower than using just PHP and "compiling" the template doesn't seem to make that much of a difference (and would prevent using separate web/HTML and code/PHP servers). While using PHP itself as a template engine might work ok for me as a single developer it will not work in an environment with separate web designers.
So to summarize I am kind of looking for/to create a combination of a MVC-framework and template engine/system that will facilitate HTML and code separation and view and model/controller separation that will actually be faster than just using a single tier. Or, more accurately, have better scalability than a single tier (although I wouldn't expect much of a speed decrease, if any, for single pages).
回答1:
Have you looked into one of the gazillion PHP frameworks around? Most of them sport all of this in one form or another. Heck, I've written a few myself, although with a strict XML templating engine to get away from greedy PHP developers. :) (xSiteable if you want to peek at the code)
MVC is nice and all, but there's many ways of skinning this cat, and to be frank, MVC (or any of its many incarnations, mutations and variations) gives separation at the cost of complexity, so you'll certainly not make it faster per se. The only thing I can think of is that your template engine spits out (ie. writes to disk in a cached fashion) pure PHP files based on backend business logic, and then you can use various accelerators with good use. You still have to decide on what the templating environment should be, though. HTML with interspersed notation or PHP, XML, or something else?
A compiler is easy enough to make, but I'm a bit wary. I doubt you'll get much speed improvements (at least not compared to the added complexity) that way over a well cached templating engine, but it's certainly doable.
But why not just use PHP itself, and simple Apache rewrite rules (using 'uri' as parameter)?
$theme = 'default' ;
$dbase = 'mysql; '
$logic = $_REQUEST['uri'] ; // or some other method, like __this__ with starting folder snipped
include 'themes/top.html' ;
include 'logic/'.$logic.'/header.php' ;
include 'themes/mid.html' ;
include 'logic/'.$logic.'/menu.php' ;
include 'themes/section1.html' ;
include 'logic/'.$logic.'/section1.php' ;
include 'themes/section2.html' ;
include 'logic/'.$logic.'/section2.php' ;
include 'themes/section3.html' ;
include 'logic/'.$logic.'/section3.php' ;
include 'themes/bottom.html' ;
include 'logic/'.$logic.'/footer.php' ;
include 'themes/end.html' ;
It's brute, fast, and does provide what you want, although it's not elegant nor pretty nor, uh, recommended. :)
来源:https://stackoverflow.com/questions/6453342/php-code-tier-and-html-view-tier-separation