2

问题总结

我正在制作的 MVC 框架中的路由存在一些小问题。代码看起来应该可以工作,但是我将请求的 uri 拆分为数组并将其分配给字符串变量的方式存在问题。我消除了最初出现的所有错误,所以我继续实例化控制器,这就是我得到的:

错误日志

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

索引.php

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

//routing
$router = new Router(array($request)); //routes the requested uri
$router->route();

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

请求.php

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);
    $j=0;
    for($i=2;$i<$count;$i++) {
        $request['args'][j] = $request[i];
        $j++;
    }

    return($request);
    }
}

路由器.php

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;
}
}



任何帮助表示赞赏!

4

2 回答 2

2

您遇到的问题是由于您尝试分配 to 的属性的Request方式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']
    //...
}

OR

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;
于 2013-08-13T00:01:21.227 回答
0

The array_filter(); function always preserves the array keys.

When used in the above variable, ($uri) created an array where the current request is stored beginning with $uri[1] not $uri[0] - that key still exists; however, it is preserved by the callback.

You can fix this by adding array_merge() to recreate the indexes, which will reset your array and correctly store the controller.

//separates the uri into an array
$uri = explode('/',$_SERVER['REQUEST_URI']);
$uri = array_merge(array_filter($uri)); // resets array

Did that help anything?

于 2013-11-02T04:57:50.787 回答