Zend Framework链接和路径错误


Zend Framework links and path error

我在Zend Framework中遇到了这个问题,我希望我的headLink和headScript像:

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

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

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

用户是我的一个控制器,配置文件是Actions,另一方面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是控制器区域的Action。

有什么方法我可以直接链接到:

http://locahost/mysite/public/

这样,像上面提到的链接就可以很容易地指向公共路径。在不影响这些链接的情况下,我将上传到实时服务器。

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

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

隐含地引用相对的url。所以,如果你在页面上:

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

浏览器将相对地址CCD_ 1解释为相对于当前页面。

BaseUrl视图助手可以缓解这个问题:

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

类似的AJAX修复方法也适用。在视图脚本中,请确保通过baseUrl()视图辅助对象运行所有直接url渲染。

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