dongyu6276 2013-02-23 02:59 采纳率: 0%
浏览 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/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度