1

我尝试了许多修复,但由于某种原因,我的框架中的请求和路由机制无法正常工作。这是一个多语言站点(en/fr),我使用 mod_rewrite 来获取语言、类、方法和可选参数。如果未设置语言,我的 request.php 将控制器设置为 languageController,并且它可以工作(成功地在控制器中回显“语言”)。但是,如果设置了语言,尽管 uri 是 /en/about,但总是最终显示的控制器是 homeController。我不知道为什么会这样,这真的让我很沮丧。这是问题代码,不胜感激。

注意:我没有使用命名空间,因为它是一个小型网站,我只想启动并运行它并且没有真正的需要,尽管我计划编辑它以在将来使用命名空间只是为了学习。

index.php 的相关部分

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

//routing
$router = new Router($request); //stores the requested uri in the router
$router->route(); //sets the proper route

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

请求.php

class Request {
    private $request;
    public function __construct($uri) {
        $this->request = $uri;
        //if the language isn't selected
        if($this->request['language']=="") {
            $this->request['language'] = "language";
            $this->request['class'] = "language";
            $this->request['method'] = "index";
        }
        //else if the controller is not selected
        else if($this->request['controller']=="") {
            $this->request['class'] = "home";
            $this->request['method'] = "index";
        }

        //stores the requested method
        else if($this->request['method']=="") {
            $this->request['method'] = "index";
        }
        //stores a value of false if there is no argument in the uri
        if($this->request['arg']=="") {
            $this->request['arg'] = false;
        }
    }
    public function getLanguage() {
        return $this->request['language'];
    }   
    public function getClass() {
        return $this->request['class'];
    }   
    public function getMethod() {
        return $this->request['method'];
    }
    public function getArgument() {
        return $this->request['arg'];
    }
}

路由器.php

class Router {
    private $language;
    private $controller;
    private $view;
    private $method;
    private $arg;
    public function __construct(Request $request) { //given an instance of request, store it in local variables
        $this->language = $request->getLanguage();
        $this->controller = $request->getClass() . 'Controller';
        $this->view = $request->getClass() . 'View';
        $this->method = $request->getMethod();
        $this->arg = $request->getArgument();
    }
    public function route() { //put the requested uri into forms we can deal with
        if($this->language == "fr") { //compare it against url translation chart
            //run all uri values through a chart that translates them from french to english if it finds a match
        }
        else { //it must be english

        }
    }
    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 getArgument() {
        return $this->arg;
    }
}
4

0 回答 0