2

我对 Zend Framework 有这个问题,我希望我的 headLink 和 headScript 像:

$this->headLink()->appendStylesheet('styles/my.css');

但是当我查看视图的页面源然后单击css的链接时,链接将是这样的:

http://localhost/mysite/public/user/profile/id/styles/my.css

用户有我的一个控制器,配置文件属于操作,另一方面,id 只是一个参数。该链接没有指向公众。

另一个问题是,当我的一个视图上有一个 AJAX 脚本时,它会从另一个 URL 获取数据,例如:

$(document).ready(function(){
      ...
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: '../region/getcountryregions',
                data: { id : test_id },
                success: function(result){
                     // success
                }, error: function(request, status, error){
                    console.log(request.responseText);
                }
      ...
});

它在我的这个网址上完美运行:

http://localhost/mysite/public/user/registration

但是当 URL 像这样时:

http://localhost/mysite/public/user/profile/id/20

上面的两个视图都具有与 AJAX 相同的脚本标签。问题是第二个链接将 URL 指向:

http://localhost/mysite/public/user/profile/region/getcountryregions

这是一个错误,因为 region 是一个控制器,而 getcountryregions 是控制器区域的一个动作。

有什么办法可以将链接指向:

http://locahost/mysite/public/

这样就可以轻松地将上述链接定向到公共路径。当我将它上传到实时服务器时,不会影响这些链接。

4

1 回答 1

3

这是一个标准的相对与绝对 URL 问题。您的调用:

$this->headLink()->appendStylesheet('styles/my.css');

隐含地指一个相对url。因此,如果您在页面上:

http://localhost/mysite/public/user/profile/

然后浏览器将相对地址styles/my.css解释为相对于当前页面。

BaseUrlview-helper 可以缓解这个问题:

$this->headLink()->appendStylesheet($this->baseUrl('styles/my.css'));

适用于您的 AJAX 情况的类似修复程序。在您的视图脚本中,确保通过baseUrl()视图助手运行所有直接 url 渲染。

作为旁注,通常不会公开/public网址的一部分。目录本身将public被映射为虚拟主机的根。如果您在共享主机上并且无法自己指定该映射,那么有几种解决方法

于 2012-08-21T04:02:34.467 回答