Destramic Posted June 17, 2011 Share Posted June 17, 2011 hey guys im looking for design advice on my framework im trying to design...but im a bit stuck on how the routing should work. the code you will see below...basically you can create a route in the bootstrap and when the framework is executed to run it will see if the uri is a match to a route....now this is were i dunno what to do. 1. should the routing system pass the controller/action and parameters to the request then passed to the dispatcher for dispatching 2. or should the controller/action and parameters be passed straight to the dispatcher to be dispatched? well if someone could help me on which design pattern would be better and give me some advise i'd be very happy...thanks <?php class Router { protected $_uri_variable = ":"; protected $_routes = array(); protected $_request = null; public function __construct() { } public function add_route($pattern, array $route) { $this->_routes[$pattern] = $route; } public function is_route($uri) { $routes = $this->_routes; foreach ($routes as $pattern => $route) { if (array_key_exists('controller', $route)) { $controller = $route['controller']; unset($route['controller']); } if (array_key_exists('action', $route)) { $action = $route['action']; unset($route['action']); } $parameters = $route; if (preg_match_all('/(?<=\w+/', $pattern, $matches)) { foreach ($matches as $match) { $count = count($matches); for ($i = 0; $i < $count;) { $find = $match[$i]; $i++; } if (array_key_exists($find, $parameters)) { $value = $parameters[$find]; if (substr($value, 0, 1) == ':') { // get from request parameters; //$this->_request->get_parameter($find); } else { $pattern = str_replace(':' . $find, $value, $pattern); } } } } if ($uri == $pattern) { return true; } else { return false; } } } } $router = $front_controller->get_router(); $router->add_route('game/:game/leagues', array('controller' => 'leagues', 'action' => 'leagues', 'game' => 'counter-strike')); Quote Link to comment https://forums.phpfreaks.com/topic/239649-mvc-routing-design-advice/ Share on other sites More sharing options...
trq Posted June 17, 2011 Share Posted June 17, 2011 It's all up to you really. In my framework, I have the router create a command object. This command object is basically just a placeholder for the names of the module, controller & action foe the current request. I then pass this into my request object (it's going to need it anyway), which in turn passes it to the dispatch process. Quote Link to comment https://forums.phpfreaks.com/topic/239649-mvc-routing-design-advice/#findComment-1231283 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.