Front Controllers - Bad Design?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Front Controllers - Bad Design?

Post by McGruff »

Harry Fuecks has some interesting comments about front controllers http://phppatterns.com/index.php/articl ... ew/81/1/1/
.

They are criticised on grounds of speed but I think it is possible to write a zippy controller. Command hierarchies & lengthy switch cases can be slow - so don't use them. Instead, a chain of includes and a script naming convention seems to work very well with no noticable, real-world speed penalty.

The other, possibly more serious, criticism is that applications using front controllers are hard to integrate with other programs. Can't think of an answer to that except to say it's probably never going to be easy, front controller or not.

I've started trying to put together a framework which employs a front controller so it would be good to hear of your experiences with them, and if you also think it's just a bad design decision in a scripting language like php.
Last edited by McGruff on Thu Nov 06, 2003 11:22 pm, edited 1 time in total.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Excuse me for being a complete muppet but what is a Front Controller and what benifits does it have when used with PHP?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Ok I followed the link and learned what a Front Controller is.

Correct me if I'm wrong but calling something a Front Controller is just an excuse for people to make themselfs sound more intelligent than they actually are.

It's simply redirecting a page or including files based on a variable value, which most people who use PHP do anyway.

This is my first Front Controller :roll:

Code: Select all

<?
if(isset($_GET["carter"]))
{
    include($_GET["carter"]);
}
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Gen-ik wrote:Ok I followed the link and learned what a Front Controller is.

Correct me if I'm wrong but calling something a Front Controller is just an excuse for people to make themselfs sound more intelligent than they actually are.
Ouch. 8O

There are several reasons to look at a front controller.

You get a handy common node for shared tasks - the intercepting filter pattern mentioned in the article.

You can create an interface for building pages. Interfaces are good. They create standard methodologies and, hopefully, bring clarity to the code.

I think one of the most important things to learn about programming is that it's not so much about writing code that can be processed by a computer but rather about code that can be processed easily by a human brain. OOP & patterns may at first seem like a leap into unecessary obscurity & complexity (I used to moan about that myself) but in fact the reverse is the case. OOP is a way to manage complex systems.

The code I'm working on basically has a RequestHandler and a DivLoader. The DivLoader is really what led me into this. As you can probably imagine, it's a presenter object which loads html layout blocks: header, footer, sidebar, that kind of thing.

I want a system which allows common layout elements to be shared efficiently hence DivLoader is polymorphic, allowing different content to be loaded into the same layout blocks. Different flavours of DivLoader can be created to vary layout design in different site areas - and new presenter objects could be created to output the content in other formats in addition to html (Pdf_Writer?).

If I get the mix right I'll have a nice chunk of reusable code which can deal with any application which gets thrown at it.

It's all based on the front controller though, and I'm not sure if that's a good way to go.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Sorry.. I wasn't aiming that "sound more intelligent" at you directly. :)

I can understand what you are saying though and I have recently started to use a similar method of working to (a) make the sites easier to edit and maintain and (b) to cut down on loading times, especially when using a massive class.

Something similar to how I'm working now would be like this.

INDEX.PHP

Code: Select all

<?php require("inc/system.class") ?>
SYSTEM.CLASS

Code: Select all

<?
class systemClass
{
      function start()
      {
            start_session();
            if(!isset($_SESSION["ini"])) require("inc/session.mod");
      }
}

$sys = new systemClass();
$sys->start();
?>
SESSION.MOD

Code: Select all

<?
$_SESSION["ini"] = true;

function buildNav()
{
      //Some Code
}

if($x != $y) buildNav();
?>
... and so on. Basically it only loads up what it needs to instead of loading the entire systemClass every time the page is loaded.


I guess the ultimate goal is to create a 'Front Controller' that can be used in any websites that I create with only a few little tweaks here and there needed to get it working.

It might need a bit of heavy brain work to sort out the 'logic' side of things but I don't see it being mission impossible.


Actually, would you mind if I bounced a few ideas off you now and again... two brains are better than one ;)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Gen-ik wrote:Correct me if I'm wrong but calling something a Front Controller is just an excuse for people to make themselfs sound more intelligent than they actually are.
I'll comment on this seperately, because the question does have merit.

The thing is, Harry presented teh Front Controller as something that we have all probably already done anyways. He just gave it a name.

Names are important in programming. They allow us to communicate our ideas easily without having to get into detailed descriptions. Rather than have me try and explain to you what a Front Controller is, I just say "Oh, this applications is going to employ a Front Controller."

Since you know what a Front Controller is, it makes perfect sense. We don't get caught up in the boring details of how to implement software using common techniques.

This is where Design Patterns come into play. For example, if I say "These classes are all Strategy classes," a person who understands Design Patterns will know what I am talking about. Thus, I don't need to explain how they work, as the underlying principle is already apparent.

It's why we call a variable a variable. It's a common name that we use for a part in programming. If we each had a different name for it, imagine how difficult life would be then!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Gen-ik wrote:Sorry.. I wasn't aiming that "sound more intelligent" at you directly. :)
No problem. :wink:
Gen-ik wrote: Actually, would you mind if I bounced a few ideas off you now and again... two brains are better than one ;)
Sure! If you've got questions about design & theory post away.
User avatar
mysqlee
Forum Newbie
Posts: 5
Joined: Thu Mar 20, 2003 11:51 pm
Location: P.R.China

Post by mysqlee »

Good ,Good~~~!
Post Reply