Front Controller

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Front Controller

Post by psurrena »

Can someone please help explain the best way to approach a Front Controller? How would one set up multiple MySQL queries in the simplest manner?
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Not sure if this is what you want.. but you could use a wrapper class, stored procs, or keep all queries in one page, then call them as needed.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It is not clear how implementing a Front Controller and setting up multiple MySQL queries are related from your questions. Those are usually highly separated parts of an application. Can you clarify?
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

arborint wrote:It is not clear how implementing a Front Controller and setting up multiple MySQL queries are related from your questions. Those are usually highly separated parts of an application. Can you clarify?
that was exactly what I was thinking... what does a front controller have to do with mysql queries??
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

I'm asking because the idea of a front controller is new to me. So you're telling me a front controller is only for templating purposes which generally would have nothing to do with MySQL queries, right?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

For the time being, take your thought of database interaction out of the picture.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

psurrena wrote:I'm asking because the idea of a front controller is new to me. So you're telling me a front controller is only for templating purposes which generally would have nothing to do with MySQL queries, right?
no... a front controller simply presents a central point of access for all incoming requests... so all requests are handled in the same manner by the same set of code.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

It would store classes and functions?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Not for templating purposes and it does not store anything. A Front Controller centralizes the request from the browser is sent to a single script that sorts out what to do with it (based on pre-defined parameters). Probably the most confusing thing about implementing the Front Controller is that it is it is a solution to a problem with another pattern -- the Page Controller. Most patterns are solutions to problems and if you don't have the problem you don't need the pattern. The are not very helpful in isolation.

Most PHP sites use the Page Controller pattern and implement multiple PHP scripts, each as an entry point for the application. The problem occurs that as the app gets bigger or more complex you can get lots of duplicated code unless you are very disciplined. The Front Controller pattern help reduce the amount of duplicated code by providing discipline through the sturcture it provides.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Generally if you see an application which you never leave the index page:
http://www.someapplicationsite.com/inde ... tuff&id=23

That application has implemented a front controller... you never enter the application from any other point... just index.php... but your request can change... you can change the action, params, and id... and this will be handled by the front controller.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Thank you for the info. Is there any book or site that deals with this particularly well?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

search google and this site for both "php design patterns" and "front controller" both will return many many results
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

I found Understanding MVC in PHP useful when I started looking at this subject.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That was a great article. I think I myself might look into that book.
Post Reply