473,785 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple model-view -separation

I'm trying to build a fairly small (max. 10 different pages) site
using php, and it's becoming obvious that I need some kind of model
view separation.

Having done a few searches, I've come across the concept of 'template
engines', but that seems like overkill for my needs. I'm thinking I'll
just stick with php as my template language; a little embedded code in
the view pages, for loops or whatever, should be ok..

I've come up with the following 'architecture', but would like some
feedback before i start working on it.
The heart of my site will be 'index.php'. Every request goes to that
page, with a parameter for picking the target 'page'.
"index.php?a=ho me", "index.php?a=ne ws", and so on.

Based on that parameter, the controller creates an instance of a class
for handling that type of request.

An 'if-else/switch' block including the file containing the logic
(model) file, creating an instance of the class inside?
Or should I create files named "model/home.php", "model/news.php" and
include these dynamically?

How do I name the classes or whatever's inside? Same name, but
different file included might work, but sounds like poooor design.
Creating an instance of class $somestring doable?
Once the logic is loaded, I plan on calling
$requestHandler->service(), which will somehow prepare the data we
plan on displaying. Will setting a global variable do the job?

$requestHandler->service() - or should i just use a global function,
there will only be one included, but I am better off placing it in a
class, right? - also returns a string, which is the url to the correct
'view'.

This page is then include(?)d, and, using the globally declared
$myTableData, it puts up a nice page with a table of data for the
user's browser..?
Jul 17 '05
13 2769
"rush" <pi**@rush.aval on.hr> wrote in message
news:cc******** **@ls219.htnet. hr...
"Droolboy" <te*******@hotm ail.com> wrote in message
news:36******** *************** ***@posting.goo gle.com...
b) use inheritance and polymorhism (like you have different midpage gadgets, then you have several sibling classes that implement them but all of them do so through function getMidPageConte nt() )


I disagree with that. Inheritance should only be used to extend the
functionality of a class (hence the "extends" keyword). It's pointless to
try to emulate polymorphism in PHP, as it's not an end in itself but a mean
for strict typing languages like C++ to implement interfaces. In PHP 4 you
can archieve that simply by using the same method name, while PHP 5 allows
you to explicitly define an interface.

--
Obey the Clown - http://www.conradish.net/bobo/
Jul 17 '05 #11
In article <pe************ *************** *****@4ax.com>, Jochen Daum wrote:
Hi Tim,

On 5 Jul 2004 11:14:54 GMT, Tim Van Wassenhove <eu**@pi.be> wrote:
In article <cc**********@s olaria.cc.gatec h.edu>, Salil Das wrote:
The heart of my site will be 'index.php'. Every request goes to that
page, with a parameter for picking the target 'page'.
"index.php?a=ho me", "index.php?a=ne ws", and so on.

The key to this architecture is that it makes authentication and loggin
stuff very easy and comprehensive and also makes the site secure, in the
sense that you control the pages the user can see. THe user does not this 
way have complete access to ur webserver. You could block/password protect
rest of your pages


Most "architectu res" allow stuff to be logged. And allow you to control
which pages are public and which are not. So would you mind telling us
what separates this "architectu re" from others?


I agree with the others. his architecture allows you to control all
this from one point, which can be a php script, database or whatever.


But my question remains, what separates it from other "architectures" ?

If everyting has to pass through a controller, you end up with some
serious overhead when fe images are requested because most of the times
they don't need access to a database etc... And if you decide they
should not be handled by the controller, you are more or less saying
that not everything should be passed through such a controller.

So instead of using a controller script, i prefer to use the webserver
that maps request to scripts/pages. And the webserver has nice logging
functionalities too. Most of my scripts do have some "common" code. So
each script that needs this, can include/require(_once) it. No more, no
less. I know you will say: But then each scripts needs to have a line
that includes the "common" stuff. And then i answer: No, if you really
want it, you can also use the auto_prepend/ auto_append option :)

So i see the same functionalities ... But not the overhead, because i
have a only use what you need strategy.

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #12
"Chung Leong" <ch***********@ hotmail.com> wrote in message
news:xa******** ************@co mcast.com...
I disagree with that. Inheritance should only be used to extend the
functionality of a class (hence the "extends" keyword). It's pointless to
try to emulate polymorphism in PHP, as it's not an end in itself but a mean for strict typing languages like C++ to implement interfaces. In PHP 4 you
can archieve that simply by using the same method name, while PHP 5 allows
you to explicitly define an interface.


As far as I know, polymorphism has come to existance in Smalltalk (as many
other OO thingies), which is dynamically typed object oriented language, and
had nothing to do with overcomming restrictions of statically typed OO
languages. In that sense PHP as dynamically typed is much closer to some OO
tenets than some other statically typed languages as java or C++ (but php
befeore 5 had some other serious problems). Popular statically typed OO
languages have skewed (in negative way imho) what OO is, and what are it's
strenghts. As Alan Kay (this year Turning Award recipient) has said
(approx): "I have invented OO, and I can tell you I did not have C++ in
mind".

rush
--
http://www.templatetamer.com/
Jul 17 '05 #13
Hi,

Very simple:

--------------------someScript.php---------------

require_once('c lasses/MainControllerC lass.php');
$controller =& new MainController( );
$controller->handleRequest( );

----------classes/MainControllerC lass.php ----------------
class MainController {

function handleRequest() {
include('views/main.php');
}

function printSomething( ) {
//do the printing
}
}
---------------views/main.php-----------------------
<HTML>
<HEAD>
</HEAD>
<BODY>
<!-- here could be some layout -->

<?php $this->printSomething () ?>

<!-- here could be more layout -->
</BODY>
------------------------------------------------------
of course the names should be specific. The view could call more methods
on the controller. There could be methods like delegateToSubCo ntroller()
taht create a new controller and delegates the request handling. The
subcontroller could again include a view script for more layout.

Our framework works like this. For more info and a diagram see:
http://www.phppeanuts.org/site/index...principle.html

If you want to avoid the complexity of the framework,
download its source (download page) but then only look into the source
of pntUnit/index.php and whatever is called from there, and what is
called from there, etc .
PntUnit is for unittesting. It does not use the framework but follows
the MVC pattern the same way as the framework does. The idiom is a
little bit different from the above example: a subcontroller is called a
'part', creating it and delegating to it is called
print<partname> Part(), a view script is called a 'skin'.

( pntUnit can be tried on line too:
http://www.phppeanuts.org/examples/pntUnit/index.php )

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

Droolboy wrote:
I'm trying to build a fairly small (max. 10 different pages) site
using php, and it's becoming obvious that I need some kind of model
view separation.

Having done a few searches, I've come across the concept of 'template
engines', but that seems like overkill for my needs. I'm thinking I'll
just stick with php as my template language; a little embedded code in
the view pages, for loops or whatever, should be ok..

I've come up with the following 'architecture', but would like some
feedback before i start working on it.
The heart of my site will be 'index.php'. Every request goes to that
page, with a parameter for picking the target 'page'.
"index.php?a=ho me", "index.php?a=ne ws", and so on.

Based on that parameter, the controller creates an instance of a class
for handling that type of request.

An 'if-else/switch' block including the file containing the logic
(model) file, creating an instance of the class inside?
Or should I create files named "model/home.php", "model/news.php" and
include these dynamically?

How do I name the classes or whatever's inside? Same name, but
different file included might work, but sounds like poooor design.
Creating an instance of class $somestring doable?
Once the logic is loaded, I plan on calling
$requestHandler->service(), which will somehow prepare the data we
plan on displaying. Will setting a global variable do the job?

$requestHandler->service() - or should i just use a global function,
there will only be one included, but I am better off placing it in a
class, right? - also returns a string, which is the url to the correct
'view'.

This page is then include(?)d, and, using the globally declared
$myTableData, it puts up a nice page with a table of data for the
user's browser..?


Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2593
by: Eric Veltman | last post by:
Hello everyone, I've posted this question before, but got no answer, so I'll try to reformulate the question, maybe it helps :-) By the way, this is not intended as the start of an ASP.NET flamewar. Before looking at PHP, I've used ASP.NET extensively for about a year and some things that I like a lot about it : - Design and code can be easily kept separate.
20
2982
by: Griff | last post by:
Hi there I'm after some suggestions as to how one might best separate form and content in a normal run-of-the-mill web application. I'm sure whole bookshelves have been written on this, but I could use some pointers as to the most helpful roads to investigate. The situation I'd like to end up with is the ability to split web development work into two halves - programmers writing back-end logic, and web designers building the look and...
0
5334
by: Dave | last post by:
Hi, I currently have the following in order to have a little bit of horizontal spacing separation between the bullet images and the text in the list (and I'm happy with the vertical separation between the list lines): UL { list-style: url("../images/bullet-right.gif") square } LI { margin: 0.8em 10% 0.8em 0;
2
6679
by: Sandeep Raje | last post by:
By default, when I start a paragraph, I get white space before the paragraph. What CSS property can I use to conrol the white space created before the paragraph. In particular, I would like to specify no white space or some white space either as pixels, percentages or in terms of number of lines. I am looking at margin-top and padding-top. But I was wondering if there is some other CSS property that directly addresses paragraph separation.
8
1465
by: twoeyedhuman1111 | last post by:
Okay, I have this sample code: class a; class b; class b { public: private:
22
2342
by: Tommy | last post by:
Hi all. I am studying computer security, and I got this short and simple (?) c-code. Something is logical wrong in this code, and if used in the wrong hands of someone, it could be taken advantage of. Here is the code: main () { char buffer;
14
2615
by: robert | last post by:
For testing purposes I'm looking for a simple DAV server - best a python thing serving a folder tree. Don't want to install/change/setup the complex apache dav .. Found PyDav http://www.econetwork.net/~jdavis/Software/PyDAV/readme.html, but the package is old and (crucial) webdav.ini.template is missing in that package. Found "python davserver" http://www.comlounge.net/webdav/ , but it doesn't run. It uses xml libs which are not...
10
2314
by: Frank van Wensveen | last post by:
Friend, coders, fellow wage slaves, lend my your ears. I believe that in a perfect world the design of a website (or feature on a website) should be totally separated from its design and the data it serves up. I'd like some suggestions on good ways to do this, because in the real world it can be quite difficult. For example, if I'm rummaging around in a MySQL database, the table structure and the code that generates the SQL requests...
17
3241
by: allen.fowler | last post by:
Hello, Can anyone recommend a simple python template engine for generating HTML that relies only on the Pyhon Core modules? No need for caching, template compilation, etc. Speed is not a major issue. I just need looping and conditionals. Template inheritance would be a
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.