无法使用自定义路由器加载css / js文件

时间:2017-06-11 21:04:38

标签: php routing

我在自定义路由器上工作(主要是为了好玩)。 这是一项正在进行中的工作,所以欢迎任何建议!

但我的主要问题是de css / js文件不再加载了。 看起来文件的路径附加到当前URL,因此用户/(css路径)。

在Chrome和Firefox中,资源只会继续加载,不会返回任何响应/代码。如果我故意引发异常,例如改变文件路径,我确实看到了返回异常的html。

我已经做了很多调试,似乎无法弄明白。

    /**
 * @var
 */
private $routes;
/**
 * @var string
 * Todo: add this to an ini file or something
 */
private $base_route = "/index.php";

/**
 * @param $route
 * @param $callback
 * @param string $method
 */
public function addRoute($route, $callback, $method = Route::GET)
{
    $routes = $this->getRoutes();
    if (empty($routes) || !in_array($route, $routes)) {
        $this->setRoute(
            [
                "route" => $route,
                "callback" => $callback
            ], $method
        );
        return;
    }
}

/**
 * @param array $route
 * @param $method
 */
public function setRoute(array $route, $method)
{
    $this->routes[$method][] = $route;
}

/**
 * @return mixed
 */
public function getRoutes()
{
    return $this->routes;
}

/**
 * @throws \Exception
 */
public function handle()
{
    $route = $this->getURI();
    $route = str_replace($this->base_route, "", $route);

    $urlparts = explode("?", $route);
    if (count($urlparts) > 1) {
        $route = $urlparts[0];
        $query = $urlparts[1];
    }

    if ($this->isAssetRoute($route)) {
        $parts = explode("/", $route);
        foreach ($parts as $part) {
            if ($part !== "public") {
                unset($parts[$part]);
            } else {
                continue;
            }
        }
        $route = implode($parts);
        return APP_PATH . "/" . $route;
    }

    if (empty($route)) {
        $this->executeCallback("HomeController");
        return;
    }

    $exists = false;
    $routes = $this->getRoutes();
    $requested_method = $this->getMethod();

    if (!isset($routes[$requested_method])) {
        throw new Exception("404: Route {$route} does not exist with method {$requested_method}");
    }

    $declared_routes = $routes[$requested_method];
    $count_routes = count($declared_routes);

    for ($i = 0; $i < $count_routes; $i++) {
        if ($declared_routes[$i]["route"] === $route) {
            $exists = true;
            $data = $declared_routes[$i];
            continue;
        }
    }

    if (!$exists) {
        throw new \Exception("404: route {$route} does not exist!");
    }

    //Todo: replace [var]
    $route = $this->compileRoute($route);

    $this->executeCallback($data["callback"]);
}

/**
 * @return mixed
 */
private function getProtocol()
{
    return $_SERVER["HTTPS"];
}

/**
 * @return mixed
 */
public function getMethod()
{
    return $_SERVER["REQUEST_METHOD"];
}

/**
 * @return mixed
 */
public function getURI()
{
    return $_SERVER["REQUEST_URI"];
}

/**
 * @param $route
 * @param $callback
 */
public function get($route, $callback)
{
    $this->setRoute(
        [
            "route" => $route,
            "callback" => $callback
        ], Route::GET
    );
}

/**
 * @param $route
 * @param $callback
 */
public function post($route, $callback)
{
    $this->setRoute(
        [
            "route" => $route,
            "callback" => $callback
        ], Route::POST
    );
}

/**
 * @param $route
 * @return mixed
 */
public function compileRoute(&$route)
{
    $uri = explode("/", $_SERVER["REQUEST_URI"]);
    $formatted_route = "";
    foreach ($uri as $key => $param) {
        $formatted_route .= "/" . preg_replace(
                "/\[(.*)\]/", "1", $param
            );
    }
    return str_replace($this->base_route, "", $formatted_route);
}

/**
 * @param $callback
 * @throws \Exception
 */
public function executeCallback($callback)
{
    $callback_data = explode("::", $callback);

    $controller = $callback_data[0];
    if (!isset($callback_data[1])) {
        $method = "index";
    } else {
        $method = $callback_data[1];
    }

    if (!class_exists($controller)) {
        throw new \Exception("Class {$controller} does not exist!");
    }

    if (!method_exists($controller, $method)) {
        throw new \Exception("Method {$method} does not exist!");
    }

    $controller::$method();
}

/**
 * @param $route
 * @return bool
 */
private function isAssetRoute($route)
{
    return (stripos($route, "/public/assets/") !== false);
}

1 个答案:

答案 0 :(得分:0)

通过获取正确的标题(text/cssapplication/javascript来解决那些问题)来解决它,回显文件内容然后返回;

我将代码放在handle {()方法中的if ($this->isAssetRoute($route))语句中。 该语句中的所有其他代码都将被删除。