dongyu6276
2013-02-23 02:59
浏览 50
已采纳

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条回答 默认 最新

  • duanfen2008 2013-02-23 03:20
    已采纳

    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/
    
    点赞 评论

相关推荐 更多相似问题