如果我使用渲染相对路径渲染()
$app->render("./somewhere/index.html");
或
echo "<html>...</html>";
与外部的依赖关系(的JavaScript/CSS)的实际HTML网站出于某种原因苗条不能解决依赖路径 - 所以该网站无法正确加载。解决方法是$ app-> redirect(“./ somewhere/index.html”);而不是$ app-> render(“./ somewhere/index.html”); - 但随后浏览器地址栏中的URL路径也发生变化。我想避免这种情况,因为我只想显示我从URI接收到的REST请求的HTML模板 - 所以没有必要重定向到html文档并丢失REST参数。
我的问题的SSCCE:
目录结构:
- index.php
+ templates
|-- index.html
|-- dependency.js
+ Slim
|-- ...
|-- ...
的index.php:
<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new Slim\Slim();
$app->get('/:id', function ($id) use ($app) {
$app->render('index.html');
});
$app->run();
?>
/templates/index.html:
<html>
<head>
<title>SSCCE</title>
<script id="someid" type="text/javascript" src="dependency.js"></script>
</head>
<body>
<a href="javascript:func_test()">Click here</a>
</body>
</html>
/templates/dependency.js:
function func_test() {
alert("Test");
}
1.)成功: 直接访问/templates/index.html。 (即通过输入127.0.0.1/templates/index.html)。点击'点击此处'打开一个消息框。
2.)失败: 从Slim访问index.php文件(注意:我没有在web服务器上激活mod_rewrite - 所以我使用index.php /.../作为URI,即127.0.0.1 /index.php/testid)。该网站加载正确,但如果我点击'点击这里'没有任何反应。
我已经尝试过:src =“../ dependency.js”,src =“../ templates/dependency.js”的不同变体,将index.html改为一个php文件并使用src =“$ webroot /templates/dependency.js“ - 但没有任何工作。任何想法如何解决这个问题?
谢谢,你说得对!这帮助了我很多。 – Constantin 2013-02-23 11:05:22