Slim PHP中使用$ app-> render()的外部依赖关系的相对路径

If I render an actual html site with external dependencies (javascript/css) using

$app->render("./somewhere/index.html");

or

echo "<html>...</html>";

for some reason slim cant resolve the dependency paths - so the site can't be loaded correctly. An workaround is $app->redirect("./somewhere/index.html"); instead of $app->render("./somewhere/index.html"); - but then also the URL path in browsers adress bar changes. I want to avoid that, because I just want to display an HTML template for the REST request I'm receiving from the URI - so theres no point to redirect to an html document and lose the REST Parameters.

An sscce for my problem:

dir structure:

- 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.) Success: Access the /templates/index.html directly. (i.e. by entering 127.0.0.1/templates/index.html). Clicking on 'Click here' opens a message box.

2.) Failure: Access the index.php File from Slim (note: I haven't activated mod_rewrite on the webserver - so I'm using index.php/.../ as URI i.e. 127.0.0.1/index.php/testid). The html site loads correctly, but if I click on 'Click here' nothing happens.

I already tried: different variants of src="../dependency.js", src="../templates/dependency.js", changing index.html into an php file and using src="$webroot/templates/dependency.js" - but nothing worked. Any idea how to fix this?

1个回答



我认为你混淆了如何使用Slim(或者像Silex这样的其他类似的Microframework)。 您的资源(如JS / CSS /图像)不应该对您的路由网址有所了解,因为该路由网址不会直接映射到基于文件系统的资源。 应该是绝对的。 您的设置应如下所示:</ p>

  Slim / 
templates /
index.html
whatever.html
images /
js /
/dependency.js \ ncss /
</ code> </ pre>

所以你所有的js引用都应该是这样的:</ p>

src =“/ js / dependency .js“</ code> </ p>

除非你想编写某种特殊的控制器来根据模板或某些东西提供正确的资源。 但是,如果您的应用程序布局足够复杂,以至于需要这样做,那么您应该使用更像Symfony2或ZF的东西,它具有封装完整功能集的模块/包的概念。 </ p>

此外,您无法通过URL公开访问模板,因为这些模板只能由Slim控制器提供。 事实上,我通常不会在文档根目录中拥有模板。 我的正常设置通常看起来像这样( public </ code>将是webserver文档根目录):</ p>

  Slim / 
templates /
index.html \ npublic /
index.php
js /
images /
css /
</ code> </ pre>
</ div>

展开原文

原文

I think youre confusing how you would typically work with Slim (or another similat Microframework like Silex). Your assets like JS/CSS/images shouldnt be realtive to your routed url because that routed url doesnt map directly to a file system based resource. The should be absolute. Your setup should look something like this:

Slim/
templates/
  index.html
  whatever.html
images/
js/
  /dependency.js
css/

So all your js references should look something like:

src="/js/dependency.js"

Unless you want to write some kind of special controller to serve up the correct resources based on a template or somethings. But if your application layout is complex enough that something liek this is required, you should probably be using something more like Symfony2 or ZF that has a concept of modules/bundles that encapsulate complete sets of functionality.

Additionally, you shouldnt be able to access templates publicly by URL because those should only be served by your Slim controllers. In fact i normally wouldnt have the templates inside the document root. My normal setup usually looks something like this (public would be the webserver document root):

Slim/
templates/
  index.html
public/
  index.php
  js/
  images/
  css/

dongpanbo4727
dongpanbo4727 谢谢,你是对的! 这对我帮助很大。
7 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐