php - Getting the parameters out of uri -
i setup simple router class extracts following
- controller
- action
- parameters
class router {
private $uri; private $controller; private $method; private $params; public function __construct($uri) { $this->uri = $uri; $this->method = 'index'; $this->params = array(); } public function map() { $uri = explode('/', $this->uri); if (empty($uri[0])) { $c = new config('app'); $this->controller = $c->default_controller; } else { if (!empty($uri[1])) $this->method = $uri[1]; // how parameters?? } } }
that simple $router->map() can give me right controller, action , single parameter uri http://domain.com/users/edit/2
that quite okay, if needed store more parameters in url : http://domain.com/controller/action/param/param2/param3
how push them $parms if don't know how many parameters passed.
you know 1st 2 values of array controller , action, after param.
so use array_shift($uri) first 2 , remaining $uri params.
public function map() { $uri = explode('/', $this->uri); // shift element off beginning of array. $controller = array_shift($uri); $action = array_shift($uri); // $uri variable not contain params. if (empty($uri[0])) { $c = new config('app'); $this->controller = $c->default_controller; } else { if (!empty($uri[1])) $this->method = $uri[1]; // how parameters?? } }
Comments
Post a Comment