Aaron Aaron - 1 year ago 105
PHP Question

PHP MVC Routing -> How do I convert uri array elements to a string useful for routing?

Summary of Issues

I'm having some minor issues with my routing in an MVC framework that I am making. The code looks like it should work, but somewhere there is something wrong with the way I've split up the requested uri into an array and assigned it to a string variable. I eliminated all of the errors that originally popped up, so I moved on to instancialize the controller and this is what I get:

Error Log

Class name must be a valid object or a string in /home2/canforce/public_html/index.php on line 23


//analyze request
$request = new Request(); //breaks up the uri into an array

$router = new Router(array($request)); //routes the requested uri

//instancialize and execute the controller
$controller = new $router->getController();
$method = $router->getMethod();


class Request {
public function __construct() {
//separates the uri into an array
$uri = explode('/',$_SERVER['REQUEST_URI']);
$uri = array_filter($uri);

//stores the requested controller from the uri
if($uri[0]!="") {
$request['controller'] = $uri[0];
else {
$request['controller'] = "home";//defaults to home

//stores the requested method from the uri
if(isset($uri[1])) {
$request['method'] = $uri[1];
else {
$request['method'] = "index";//defaults to index

//stores the requested arguments in array form
$count = count($uri);
for($i=2;$i<$count;$i++) {
$request['args'][j] = $request[i];



class Router {
private $language = NULL;
private $controller;
private $view;
private $method;
private $args = NULL;
public function __construct($request) { //given the requested uri in array form, stores it in local variables
$this->controller = $request['controller'];
$this->method = $request['method'];
$this->args = $request['args'];
public function route() { //put the requested uri into forms we can deal with

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 Help is Appreciated!

Answer Source

The issue you are having is due to the way in which you are trying to assign the properties of Request to Router.

$router = new Router(array($request));

Here you have given the Router its argument as an array with one element. The Request instance. You are not casting to an array. I'm not sure if that was the intent. Because of this you are not able to access the properties of Request in array notation (such as $request['controller]).

As is, the Router would need to be modified so Router::__construct() correctly assigns the parameters. e.g.

public function __construct($array) 
    $request = $array[0]; 
    $this->controller = $request->getController(); // No longer using array notation
    $this->method = $request->getMethod();         // such as $request['method']


Remove the unneeded array wrapping the request argument and type hint on the argument in the 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 as $request['method']

Also you will need to store a the class name string before you create your new instance:

$className = $router->getController();
$controller = new $className;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download