views:

162

answers:

1

Hi! I have a following Kohana setup:

All my files are placed under 'public_html/koh' My js files are placed under 'public_html/koh/media/js/'

I use html::script helper to include those javascript files which generates me following html code:

<script type="text/javascript" src="/koh/media/js/site.js"></script>

In my js I access one of controllers like 'json/getsomething' (which is http://localhost/koh/json/getsomething).

It works OK as long as I'm staying in top of the controller: http://localhost/koh/home

When I go to 'http://localhost/koh/home/index' it renders the same page of course but 'json/getsomething' is not accessible from Javascript anymore.

How can I solve this problem?

Include Javascript using absolute path? Create a variable in js like var fullPath = 'http://localhost/koh/'?

What is the best practice to do it?

Leonti

+1  A: 

That's how I did it.

I made a function url_base which would correspond to kohana's url::base and so it would switch around when I moved from localhost and to production.

View template:

<script type="text/javascript">
    function url_base() { return "<?php echo url::base();?>"; }
</script>

And then in config.php:

if(IN_PRODUCTION) {
    $config['site_domain'] = '/';
}
else {
    //if in localhost redirect to localhost/mysite
    //instead of just localhost
    $config['site_domain'] = '/mysite/';
}
Jonas
Works like a charm! Exactly what I needed!
Leonti