动态路由未正确链接到 CSS 和 JS 文件
Dynamic Routing not properly linking to CSS and JS files
我目前正在尝试在 Fat-Free-Framework 3.6 - but I dislike the idea of having to manually add all my basic routes to routes.ini
, and I decided I wanted to try my hand at more dynamic routes 中设置一个项目。
为此,我制作了 2 条新的 GET 路线。这是我拥有的:
$f3->route('GET /@module','{@module}_controller->index');
$f3->route('GET /@module/@method','{@module}_controller->@method');
第一个是加载控制器和它的默认方法(我在这些示例中已经index
)。
第一条路线很好用,如果我将浏览器导航到 http://example.com/ex1
,它将加载控制器 ex1_controller
和 运行 索引方法,一切都会 运行并正常显示。
然而,第二条路线没有按预期工作。在第二条路线中尝试 运行 完全相同的方法时,我没有得到 CSS。例如,如果我现在导航到 http://example.com/ex1/index
,页面上没有 CSS - 但是当我检查页面源代码时,我看到 CSS 应该加载为 [=20] =]在页面中。
经过进一步调查,我按照 link 找到了源代码中的 CSS 文件,这让我找到了不正确的 link。在第二条路线中,出于某种原因,它没有尝试从根目录加载。它试图从 URL ./ex1/assets/css/uikit.min.css
加载,但 CSS 实际上存在于 ./assets/css/uikit.min.css
结构:
index.php - Where routes are defined
| app
| controllers
Controller.php
ex1_controller.php
| models
ex1.php
| views
| ex1
index.htm
| assets
| css
uikit.min.css
| js
uikit.min.js
ex1_controller.php
<?php
class ex1_controller extends Controller {
function index() {
$example = new ex1($this->db);
$output = $example->returnOutput();
$this->f3->set('output', $output);
echo $this->template->render('ex1/index.htm');
}
}
Controller.php
文件在每条路线上都是运行,然后由模块的控制器扩展。这是加载 main_header.htm
文件的地方,其中包含 css/js links。这就是 link 在 main_header.htm
;
中的定义方式
<link rel="stylesheet" type="text/css" href="assets/css/uikit.min.css">
<script src="assets/js/uikit.min.js"></script>
<script src="assets/js/uikit-icons.min.js"></script>
为什么 F3 在使用诸如此类的动态路由时尝试从错误的目录加载 CSS/JavaScript,我该如何解决?
解决这个问题似乎很容易。
只需在链接资源路径的开头添加一个斜杠。
<link rel="stylesheet" type="text/css" href="/assets/css/uikit.min.css">
<script src="/assets/js/uikit.min.js"></script>
<script src="/assets/js/uikit-icons.min.js"></script>
我目前正在尝试在 Fat-Free-Framework 3.6 - but I dislike the idea of having to manually add all my basic routes to routes.ini
, and I decided I wanted to try my hand at more dynamic routes 中设置一个项目。
为此,我制作了 2 条新的 GET 路线。这是我拥有的:
$f3->route('GET /@module','{@module}_controller->index');
$f3->route('GET /@module/@method','{@module}_controller->@method');
第一个是加载控制器和它的默认方法(我在这些示例中已经index
)。
第一条路线很好用,如果我将浏览器导航到 http://example.com/ex1
,它将加载控制器 ex1_controller
和 运行 索引方法,一切都会 运行并正常显示。
然而,第二条路线没有按预期工作。在第二条路线中尝试 运行 完全相同的方法时,我没有得到 CSS。例如,如果我现在导航到 http://example.com/ex1/index
,页面上没有 CSS - 但是当我检查页面源代码时,我看到 CSS 应该加载为 [=20] =]在页面中。
经过进一步调查,我按照 link 找到了源代码中的 CSS 文件,这让我找到了不正确的 link。在第二条路线中,出于某种原因,它没有尝试从根目录加载。它试图从 URL ./ex1/assets/css/uikit.min.css
加载,但 CSS 实际上存在于 ./assets/css/uikit.min.css
结构:
index.php - Where routes are defined
| app
| controllers
Controller.php
ex1_controller.php
| models
ex1.php
| views
| ex1
index.htm
| assets
| css
uikit.min.css
| js
uikit.min.js
ex1_controller.php
<?php
class ex1_controller extends Controller {
function index() {
$example = new ex1($this->db);
$output = $example->returnOutput();
$this->f3->set('output', $output);
echo $this->template->render('ex1/index.htm');
}
}
Controller.php
文件在每条路线上都是运行,然后由模块的控制器扩展。这是加载 main_header.htm
文件的地方,其中包含 css/js links。这就是 link 在 main_header.htm
;
<link rel="stylesheet" type="text/css" href="assets/css/uikit.min.css">
<script src="assets/js/uikit.min.js"></script>
<script src="assets/js/uikit-icons.min.js"></script>
为什么 F3 在使用诸如此类的动态路由时尝试从错误的目录加载 CSS/JavaScript,我该如何解决?
解决这个问题似乎很容易。
只需在链接资源路径的开头添加一个斜杠。
<link rel="stylesheet" type="text/css" href="/assets/css/uikit.min.css">
<script src="/assets/js/uikit.min.js"></script>
<script src="/assets/js/uikit-icons.min.js"></script>