gpt4 book ai didi

php - 路由器控制 CSS 和图像

转载 作者:行者123 更新时间:2023-11-28 15:42:55 25 4
gpt4 key购买 nike

一直在关注 PHP 系列,但在尝试加载图像和样式表时遇到了障碍。

所以看起来路由器正在为所有东西寻找路由

#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.');
}
}

我的路线是这样的

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

这方面的任何帮助都会很棒

编辑我的 htaccess 文件```

RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteRule ^(.+)$ index.php [QSA,L]

```

最佳答案

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

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteRule ^(.+)$ index.php [QSA,L]

如您所见,我们删除了 RewriteBase/ 行。

我的猜测是这一行导致 Assets URL(/css/js 等)无法被 2 识别为文件或目录>RewriteCond 条件。这意味着这些请求被重定向到 index.php

编辑

除了我上面的回答,还发现了一个更稳定的 .htaccess 设置;

Options +FollowSymLinks 
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

关于php - 路由器控制 CSS 和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43248989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com