What is the point of a controller? I’ve never had a good explanation of that; and code I’ve seen for them seems to be something that could have been rolled into a different file, and mostly didn’t need a separate class to do either.
Apart from seeing them around on the web (all these files called SomeLongStringController.jsp) I first came across them mentioned on the “SitePoint forums”:http://www.sitepointforums.com/[1] and then in “Booby”:http://www.nauta.be/booby/
So, can anyone explain? Or is it a fashionable way to overdesign your program, to keep in with the latest Java OO crowd (and those who think Java has to be best and PHP should be programmed like it)? Is it suited to data-centric modelling (not totally sure of that term’s definition, but I believe that’s what I drift towards)?
fn1. I can never decide if people there know what they’re talking about, or have a little knowledge and he who talks loudest/can be bothered to post gets set up as an expert - as I don’t see many active in mainstream PHP projects. Which is why when they start using Controllers I’m a little suspicious ![]()
6 comments ↓
I dunno, I don’t pay too much attention to “architecture astronauts”:http://www.joelonsoftware.com/articles/fog0000000018.html, so I’m not really familiar with what you’re talking about. Controller means what in this case?
Think word “controller” is used so developers have a common word, allowing them read from the same song sheet. In general I guess it means any code that deals with directing the flow of logic in an application.
In PHP the most common use is probably a switch statement which includes a file based on a $_GET variable.
Once the Java crowd and various patterns commentators got in on the web app scene, further names got defined for different types of controller e.g.;
Front Controller: http://wact.sourceforge.net/index.php/FrontController (regarded as the “central point” in Java web apps)
Page Controller: http://wact.sourceforge.net/index.php/PageController (a controller for, perhaps, a single form)
Application Controller: http://wact.sourceforge.net/index.php/ApplicationController (a controller for, perhaps, a form which has multiple pages - a “Wizard”)
Re Sitepointforums - you have to judge for yourself. Alot of what happens there is experimental and it’s useful as a ground for exploring PHP’s use. There’s certainly people there that know what they’re doing (read - are making real money from developing PHP apps) so perhaps thats a balance to those that dont.
bq. Think word “controller” is used so developers have a common word, allowing them read from the same song sheet. In general I guess it means any code that deals with directing the flow of logic in an application.
bq. In PHP the most common use is probably a switch statement which includes a file based on a $_GET variable.
So why not just say so? It might be a common word so that all developers in the know can talk to each other, but it’s still jargon.
<rant>
What’s this need to use jargon in the computer world for everything? Just to make it sound more impressive than it really is? Having waded through some papers (”1″:http://eprints.ecs.soton.ac.uk/archive/00008672/, “2″:http://eprints.ecs.soton.ac.uk/archive/00007677/, “3″:http://eprints.ecs.soton.ac.uk/archive/00008805/, “4″:http://eprints.ecs.soton.ac.uk/archive/00009084/, “5″:http://eprints.ecs.soton.ac.uk/archive/00008803/) that are stuffed full of fancy jargon and flowery, pointless language to prepare for today’s exam this is one of my pet peeves at present. What’s gone wrong with plain speaking? I know it doesn’t sound so impressive, doesn’t make people go “Wow, that sounds clever!”, but hey, the people who are clever enough to understand what you write know it’s all hot air - and think you a jerk for being so stuck up and pompous.
</rant>
bq. Re Sitepointforums - you have to judge for yourself. Alot of what happens there is experimental and it’s useful as a ground for exploring PHP’s use. There’s certainly people there that know what they’re doing (read - are making real money from developing PHP apps) so perhaps thats a balance to those that dont.
I don’t like it when I see people saying “This is the only way you should do something”. Sometimes it’s the case; most often it’s the one they’ve been taught, or it contains the latest buzzword and has been heavily promoted. It’s usually good advice, generally given with the best of motives, but in many cases people are blinkered to the one approach. That’s what I don’t like to see - on some of the threads people I read people were told they were wrong, because they weren’t treating PHP like Java (these were threads relating to the Eclipse library and related topics). Fact is, PHP _isn’t_ Java, and while borrowing some things are great (I am a fan of OO programming, although more because it keeps the namespace clear and groups relevant functions than because it models real world objects) I don’t see why a Java architecture should suit. Although I’d love to see a PHP server widely used that will do the equivalent of loading the script once, and not reloading it until the server was restarted (at least that is my understanding of some Java setups). I’ve grown up with the PHP model for web applications, but I can see big benefits for bigger applications from this way.
The controller pattern takes the logic for “what should happen under X conditions” and bumps it up to the level above the conditions.
In a web application, maybe many different conditions all lead to the same error page. Or the same checkout page. Instead of sprinkling the logic throughout your app to handle this, some frameworks allow you to move the logic up, above the pages, to a “Controller” object.
Another example. Imagine a form where input in any one of several fields enables another set of fields. But only under some other variable condition.
Instead of having event handlers for each one of the inputs, you’d want to move this logic up — to a FormController. Its’s just easier that way.
This is a very sensible and natural thing to do; so much so that I imagine most programmers would do it all by themselves given time to discover it.
The general value of patterns are that a) they give us a new set of known good solutions to hard problems, and b) they give us a common vocabulary to use when designing applications.
With that said, it’s just a tool, and it can be used wrongly. It’s up to each programmer to decide whether the tool is adding value or not.
As you’ve mentioned PHP is a very powerful language in it’s own right - especially for problems which are not “hard”. Maybe forcing PHP to be Java indicates we are using the wrong tool again.
bq. This is a very sensible and natural thing to do; so much so that I imagine most programmers would do it all by themselves given time to discover it.
When Harry posted his PHP example, my first thought was “Oh, so that’s a controller. I’ve been using that for months”.
This afternoon I’ve been editing files on a site I built over a year ago, wishing I’d used controllers for it (at the time I was in “Separate page for everything” mode - what a pain to maintain!). It’s good to see my programming has progressed
Hi,
I am the maintainer of the ["Booby":http://www.nauta.be/booby] project. I am a Java developer so the reason why there are controllers in my application is probably a bit of “If all you know is a hammer….”.
However, the controller is not a typical Java thing. It is part of the Model-View-Controller (MVC or Model II) design pattern that was used to build interfaces in Smalltalk. The book “Design Patterns - Elemenst of Reusable Obejct-Oriented Software” by the Gang of Four has an introduction on this.
Barry
Leave a Comment