路由器控制CSS和图像

时间:2017-04-06 07:51:45

标签: php css

当我尝试加载图片和样式表时,一直关注PHP系列并遇到麻烦。

所以看起来路由器正在寻找一切的路线

version: "3"

services:
  db:
    image: db
    volumes:
      #1
      - data-volume:/var/lib/db
  backup:
    image: backup-service
    volumes:
      #2
      - data-volume:/var/lib/backup/data

volumes:
  data-volume:

正如您所看到的,它无法加载任何此类

的路线

这是路由器类

#0 /Users/Graham/Projects/OGPokedex/index.php(7): Router->direct('css/bootstrap.c...')
#1 {main}
  thrown in /Users/Graham/Projects/OGPokedex/core/Router.php on line 21
[06-Apr-2017 09:08:28 Europe/Berlin] PHP Fatal error:  Uncaught Exception: No route defined for this URI. in /Users/Graham/Projects/OGPokedex/core/Router.php:21
Stack trace:
#0 /Users/Graham/Projects/OGPokedex/index.php(7): Router->direct('css/bootstrap.c...')
#1 {main}
  thrown in /Users/Graham/Projects/OGPokedex/core/Router.php on line 21
[06-Apr-2017 09:08:28 Europe/Berlin] PHP Fatal error:  Uncaught Exception: No route defined for this URI. in /Users/Graham/Projects/OGPokedex/core/Router.php:21
Stack trace:
#0 /Users/Graham/Projects/OGPokedex/index.php(7): Router->direct('media/logo.png')
#1 {main}
  thrown in /Users/Graham/Projects/OGPokedex/core/Router.php on line 21
[06-Apr-2017 09:08:28 Europe/Berlin] PHP Fatal error:  Uncaught Exception: No route defined for this URI. in /Users/Graham/Projects/OGPokedex/core/Router.php:21
Stack trace:
#0 /Users/Graham/Projects/OGPokedex/index.php(7): Router->direct('js/bootstrap.mi...')
#1 {main}
  thrown in /Users/Graham/Projects/OGPokedex/core/Router.php on line 21

我的路线看起来像这样

<?php

class Router {
    protected $routes = [];

    public static function load($file) {
        $router = new static;
        require $file;
        return $router;
    }

    public function define($routes){
        $this->routes = $routes;
    }

    public function direct($uri){
        if (array_key_exists($uri, $this->routes)){
            return $this->routes[$uri];
        }

        throw new Exception('No route defined for this URI.');
    }
}

对此有任何帮助都很棒

编辑 我的htaccess文件 ```

$router->define([
    '' => 'controllers/index.php',
    'about' => 'controllers/about.php',
    'culture' => 'controllers/about-culture.php',
    'contact' => 'controllers/contact.php',
    'search' => 'controllers/search.php'
]);

```

1 个答案:

答案 0 :(得分:1)

在聊天中快速讨论之后,我们发现htaccess导致了这个问题。 htaccess文件需要更改为;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="width"></div>
<div id="height"></div>

如您所见,我们删除了RewriteEngine On RewriteCond %{REQUEST_FILENAME}% !-d RewriteCond %{REQUEST_FILENAME}% !-f RewriteRule ^(.+)$ index.php [QSA,L] 行。

我的猜测是,此行导致资产网址(RewriteBase //css等)无法被2个/js条件识别为文件或目录。这意味着这些请求被重定向到RewriteCond

修改

根据我上面的回答,找到了更稳定的index.php设置;

.htaccess