DarkWater Posted October 20, 2008 Share Posted October 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 20, 2008 Author Share Posted October 20, 2008 Also, would it still be considered a Strategy if the strategy is determined and instantiated inside of the class instead of being passed in? Quote Link to comment Share on other sites More sharing options...
discomatt Posted October 20, 2008 Share Posted October 20, 2008 I'm not entirely sure why you want your script to re-parse the request URI when PHP has already done it for you. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 20, 2008 Author Share Posted October 20, 2008 I'm not entirely sure why you want your script to re-parse the request URI when PHP has already done it for you. Parse out the controller, action, and parameters for the request... (this is basic MVC pattern stuff...) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 21, 2008 Author Share Posted October 21, 2008 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()); ?> Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 21, 2008 Share Posted October 21, 2008 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). Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 21, 2008 Author Share Posted October 21, 2008 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.) Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 21, 2008 Share Posted October 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 21, 2008 Author Share Posted October 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 22, 2008 Share Posted October 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 22, 2008 Author Share Posted October 22, 2008 K, I'll research that tomorrow. Thanks. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 23, 2008 Author Share Posted October 23, 2008 I didn't get much a chance to research it yet (life's been hectic), so just a little bump. If anyone has any information that they'd like to share on how Zend does their routing and dispatching, that would be great. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.