2010-04-18 26 views
2

我有以下Kohana的设置:Kohana的和Javascript路径

我所有的文件都放在下 '的public_html/KOH' 我的js文件被放置在 '的public_html/KOH /媒体/ JS /'

我用html ::脚本帮手,包括那些产生了我下面的HTML代码JavaScript文件:

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

在我的js我访问诸如“JSON/getsomething”(这是http://localhost/koh/json/getsomething)控制器之一。

它的工作原理确定只要我住在控制器的顶部: http://localhost/koh/home

当我去“http://localhost/koh/home/index”它呈现过程,但“JSON/getsomething”的同一页面是不是从访问Javascript了。

我该如何解决这个问题?

包括JavaScript使用绝对路径? 在js中创建一个变量,如var fullPath ='http://localhost/koh/'?

这样做的最佳做法是什么?

Leonti

回答

4

这就是我做到的。

我做了一个函数url_base,它对应于kohana的url::base,所以当我从本地主机移到生产时它会切换。

查看模板:

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

然后在config.php:

if(IN_PRODUCTION) { 
    $config['site_domain'] = '/'; 
} 
else { 
    //if in localhost redirect to localhost/mysite 
    //instead of just localhost 
    $config['site_domain'] = '/mysite/'; 
} 
+0

工程就像一个魅力!正是我需要的! – Leonti 2010-04-18 19:37:35

2

(有点迟,但希望仍然有用)下面是我用它来更好地组织我的另一种方式JS-服务器端瓦尔。

我在某处开始了一些基本变量 - 例如。在 “前()” 函数:

$this->template->appconf = array(
     'url_base' => url::base(), 
     'l' => substr(I18n::$lang, 0, 2), 
    ); 

现在,每当我需要任何额外的变量,我可以添加:

$this->template->appconf['page_key'] = 'product_page'; 

最后在模板这个cleaniness:

<script type="text/javascript"> 
    var appconf = <?php echo json_encode($appconf); ?>; 
</script> 

使用这样:

<script type="text/javascript"> 
    console.log(appconf.url_base); // "/mysite/" 
</script>