views:

635

answers:

2

I've been researching PHP frameworks as of late for some personal projects, and it looks like most of them use a front controller to mimic a response. The controller gets the params from the request, and re-routes by sending the appropriate headers depending on the logic. This is the "response". Is this the best way to do this in PHP, or are there other theories about how to handle re-routing and responses?

+3  A: 

a front controller lends itself quite well to a web environment, allowing you to funnel all requests to your application. since HTTP is stateless, and a user can, in a sense, inadvertently stumble upon parts of your app by accident (ie, hitting random URL's), a front controller allows you to determine the entry point of your application, and respond appropriately.

edit: in response to the comments, i think the confusion may be that java has a lot more structure to it than PHP, which might be overcomplicating the whole thing? ultimately PHP can provide for the very basic interaction from request to response:

switch($_GET['page']) {
  case "one";
      print "page one!";
      break;
  default:
      print "default page";
      break;
}

and from there you can layer in all sorts of things to front controllers passing request objects down a filter chain to a page controller which reroutes to the appropriate model which grabs data via your db abstraction layer, filters it, back up to the controller, and on to the view which constructs the appropriate response, all the while firing off random event hooks. ultimately it's up to you (as developer) to choose what level of complexity/separation you're looking for. this is both the beauty and evilness of PHP :)

Owen
+1  A: 

I think you are confusing an Http response with a response object in the frameworks you looked at. A front controller is the gateway for your application - all (http) requests go through it, and it routes to the appropriate controller/action. Processing a request does not necessary results in a returned response (often requests are only meant to send information to the server), however all requests would have passed through the Front Controller.

A request object is often used to encapsulate the environment and http request parameters and provide an API to retrieve them. Its complement, the response object, is often used to encapsulate the process of generating an http response, including the generation headers.

There are other approaches to handling requests and routing, which are not unique to PHP (and neither is the front controller), such as a Page Controller, or not using an MVC structure at all.

Eran Galperin
Can you expand any on the Page Controller idea as it applies to routing?
hal10001
The page controller is pattern in which no router or front controller involved. Usually a simplistic approach such as an if .. else block or a switch decides which page to load. It works for simple cases. Check out this article - http://www.onlamp.com/pub/a/php/2004/10/14/page_controller.html
Eran Galperin