dtng25909 2015-01-19 09:18
浏览 63
已采纳

RequireJS with PHP和漂亮的URL

I've been trying to solve this issue for hours now and wasn't able to find solution on my own nor on the internet, so finally I gave up and decided to post a question.

I'm trying to build my new PHP app with javascript managed as AMD, using RequireJS. It is my first time using this approach and I'm really excited about it, but there is one problem that keeps my javascript from functioning.

It is nice url done by routing. (I use Nette Framework for routes and Apache as webserver). After I set up requrejs and my dependencies, everything works fine with the homepage (localhost/), but when I go to some other page, which has URL something like localhost/news, it stops working and requirejs complains about nonexisting scripts in for example "localhost/testapp/news/js/vendor/jquery/dist/jquery.js". It should instead look in "localhost/testapp/js/vendor/jquery/dist/jquery.js", but it takes the argument (in this case presenter name) rewriten by mod_rewrite as part of the base url. I believe that the fault is somewhere on my side, since this has to be very common setup and I was not able to find simillar question.

requirejs in layout.latte (base template):

<script src="{$basePath}/js/vendor/requirejs/require.js"></script>
<script>
    var absolute_base = {$basePath};
    require([absolute_base + '/js/app.js']);
</script>

(in basePath variable in Nette framework is always the root of the app, regardless of further parameters - in this case, /testapp)

app.js

requirejs.config({
    baseUrl: 'js/modules',
    paths: {
        'jquery': '../vendor/jquery/dist/jquery',
        'ink': '../vendor/InkJS/dist/js/ink-all',
        'ajax': '../vendor/ajax'
    }
});

dir structure:

 - www
    - css
    - sass
    - graphics
    - fonts
    - js
        - modules
        - vendor
            - jquery
            - requirejs
            - ink
        - app.js
    - index.php

This is as far as I have get. I have tried lots of combinations, relative paths, dots in front of paths, data attribute for script tag etc. and haven't been able to find a solution.

Any help is greatly appreciated, cheers!

  • 写回答

1条回答 默认 最新

  • drzfz9995 2015-01-22 14:12
    关注

    I finally made it work. I found out, that it could not be done in this manner, because requirejs will always use the current url as the "root", not the URL of the script itself (used in "script" tag). At least I haven't find a way how to get around this.

    Instead, I've removed the whole app.js file and moved configuration right into the main template with php variable $basePath available, which I used to define the basePath (basicly I defined an absolute path instead of a relative one).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。