2

I have this problem with Zend Framework wherein I want my headLink and headScript to be like:

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

but when I view the page source of the view then click on the link of the css, the link would be like this:

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

user there is one of my Controllers and profile is of the Actions, id on the other hand is just a parameter. The link did not point to the public.

Another problem is that, when I have an AJAX script on one of my views, which acquires data from another URL like:

$(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);
                }
      ...
});

It works perfect on this url of mine:

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

But when the URL is like:

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

And both views above have the same script tags with AJAX. The problem is that the second link points the URL to:

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

And it is an error, since region is a Controller and getcountryregions is an Action of the controller region.

Is there any way I can direct the links to:

http://locahost/mysite/public/

So that links like mentioned above would be directed easily to the public path. And without affecting these links when I will be uploading it to a live server.

Dhagz
  • 711
  • 5
  • 24

1 Answers1

3

This is a standard relative-vs-absolute url issue. Your invocation:

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

implictly refers to a relative url. So, if you are on the page:

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

then the relative address styles/my.css is interpreted by the browser as relative to the current page.

The BaseUrl view-helper can mitigate this issue:

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

A similar fix for your AJAX situation applies. In your view scripts, make sure to run all your direct url renderings through the baseUrl() view helper.

As a side note, typically, one would not expose the /public part of the url. The public directory itself would be mapped as the root of a virtual host. If you are on shared hosting and are unable to specify that mapping yourself, then there are several workarounds.

Community
  • 1
  • 1
David Weinraub
  • 14,144
  • 4
  • 42
  • 64