PHP MVC Routing -> How do I convert uri array elements to a string useful for routing? -
summary of issues
i'm having minor issues routing in mvc framework making. code looks should work, somewhere there wrong way i've split requested uri array , assigned string variable. eliminated of errors popped up, moved on instancialize controller , get:
error log
class name must valid object or string in /home2/canforce/public_html/index.php on line 23
index.php
//analyze request $request = new request(); //breaks uri array //routing $router = new router(array($request)); //routes requested uri $router->route(); //instancialize , execute controller $controller = new $router->getcontroller(); $method = $router->getmethod(); $controller->$method();
request.php
class request { public function __construct() { //separates uri array $uri = explode('/',$_server['request_uri']); $uri = array_filter($uri); //stores requested controller uri if($uri[0]!="") { $request['controller'] = $uri[0]; } else { $request['controller'] = "home";//defaults home } //stores requested method uri if(isset($uri[1])) { $request['method'] = $uri[1]; } else { $request['method'] = "index";//defaults index } //stores requested arguments in array form $count = count($uri); $j=0; for($i=2;$i<$count;$i++) { $request['args'][j] = $request[i]; $j++; } return($request); } }
router.php
class router { private $language = null; private $controller; private $view; private $method; private $args = null; public function __construct($request) { //given requested uri in array form, stores in local variables $this->controller = $request['controller']; $this->method = $request['method']; $this->args = $request['args']; } public function route() { //put requested uri forms can deal } public function getlanguage() { return $this->language; } public function getcontroller() { return $this->controller; } public function getview() { return $this->view; } public function getmethod() { return $this->method; } public function getargs() { return $this->args; } }
any appreciated!
the issue having due way in trying assign properties of request
router
.
$router = new router(array($request));
here have given router argument array 1 element. request
instance. you not casting array. i'm not sure if intent. because of not able access properties of request
in array notation (such $request['controller]
).
as is, router
need modified router::__construct()
correctly assigns parameters. e.g.
public function __construct($array) { $request = $array[0]; $this->controller = $request->getcontroller(); // no longer using array notation $this->method = $request->getmethod(); // such $request['method'] //... }
or
remove unneeded array wrapping request argument , type hint on argument in constructor:
$router = new router($request); // router.php public function __construct(request $request) { $this->controller = $request->getcontroller(); // no longer using array notation $this->method = $request->getmethod(); // such $request['method'] //... }
also need store class name string before create new instance:
$classname = $router->getcontroller(); $controller = new $classname;
Comments
Post a Comment