Jump to content

Router + Dispatcher


DarkWater

Recommended Posts

Would you guys recommend setting up a Router that implements a Strategy pattern to parse out the controller, action, and any arguments out of a request URI?  This is so that I can be flexible in my method of accepting parameters.

 

The end result would probably look like (in the index.php file):

<?php
$r = new Router($_SERVER['REQUEST_URI']); //in the constructor, call a private method to determine the correct routing parser and make it retrievable through getRouter()
Dispatcher::callRoute($r->getRouter()->parse());
?>

 

I think. >_<  Any ideas?

 

Edit: Btw, this is my first attempt at a router and dispatcher, so I'm just brainstorming.  This is ultimately going to be implemented into a personal framework I'm going to be starting.

Link to comment
Share on other sites

Actually, I was thinking about maybe having a RouterFactory that produces the right Router with the strategy passed in (like the standard strategy pattern):

 

<?php
Dispatcher::callRoute(RouterFactory::getNew($_SERVER['REQUEST_URI'])->getRouter()->parse());
?>

Link to comment
Share on other sites

I've always liked the way the zend framework did it. They encapsulate the router/dispatcher instances in a singleton front controller class, so all you have to do is set the controller path and then tell the front controller to dispatch and it passes on the request to the router and the dispatcher.

 

The actual URI parsing is done by a request object, which the front controller instantiates and passes to the router (as well as some other objects such as plugins, etc).

Link to comment
Share on other sites

Oh, so the FrontController handles the interface to an application, and it hands everything off to the right places?  And internally, how does it handle the routing and dispatching?  (Just curious to see if you know off hand;  if not, I can go download the source code and check.)

Link to comment
Share on other sites

Hey DarkWater, not to derail the conversation, but do you have any links that describe what a Router and Dispatcher means in this context?  I've used a Front Controller before, but it was a pretty simple setup.  Commands were constructed and executed based on raw request info, parsed in my command factory object.  I'm just wondering if that's more or less the same thing.

Link to comment
Share on other sites

Hey DarkWater, not to derail the conversation, but do you have any links that describe what a Router and Dispatcher means in this context?  I've used a Front Controller before, but it was a pretty simple setup.  Commands were constructed and executed based on raw request info, parsed in my command factory object.  I'm just wondering if that's more or less the same thing.

 

Well, in this situation, it's pretty much as follows:

1. Instantiate front controller and tell it where to find everything.

2. The front controller instantiates a request object which parses all of the information out of the request URI and passes it to the router.

3. The router determines where everything should actually go.

4. The dispatcher calls the controller methods based on what the router tells it.

Link to comment
Share on other sites

Oh, so the FrontController handles the interface to an application, and it hands everything off to the right places?

 

Pretty much... between the bootstrap and the index file it's really nothing more than

 

<?php
$frontCont = Zend_Controller_Front::getInstance();
$frontCont->setControllerDirectory('whatever/the/path/is');
$frontCont->dispatch();
?>

 

or even

 

<?php
Zend_Controller_Front::getInstance()->run('whatever/the/path/is');
?>

 

at least as far as routing/dispatching goes. Of course the framework gives you 1000 other ways of dispatching.

 

And internally, how does it handle the routing and dispatching?  (Just curious to see if you know off hand;  if not, I can go download the source code and check.)

 

That I don't know offhand.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.