images in Zend Framework layout

2019-02-14 19:30发布

问题:

I am a bit lost here, solution could be lurking under my nose but I couldn't get, thought of you guys if any one can help.

Here is the problem: I have Zend Framework standard file layout:

Project
  -application
     -controllers
     -views 
     -layouts
        -scripts
           -layouts.phtml
  -library
  -public
     -images
     -index.php

Now The problem is, I am referencing images in layouts.phtml by just images/logo.gif etc. and same in the controller views /images/arrow.gif

That works fine if the request is simple http://servername/project/controller

but if the request is more deep like http://servername/project/controller/index/page/2

the images break theirselves, they clearly can't get the path, what I am noticing is, if I am on later request, the image path will be http://servername/project/controller/index/page/2/images/logo.gif which is not there, the image is in public/images

My understanding was (and I have googled it a bit as well) framework knows the default public and will route the images to public/images always. However, this is not working. Do I have to add some re-write rule or something?

Can anyone help please? I will be grateful!

回答1:

Sometimes you should first ask the person sitting next to you!

Here is the solution:

<?=$this->baseUrl('images/logo.gif')?>

Edit after further investigation

For record and to help others: You can access images folder in public as you normally would in non Zend framework environment if you have configured your DocumentRoot properly. I only made a virtual directory in my httpdconf while if you do not create a VirtualHost and do not give DocumentRoot to it while your files are also not in default htdocs directory, expect this problem. See Create a Virtual Host heading in following URL: http://framework.zend.com/manual/en/learning.quickstart.create-project.html and that will solve the problem!



回答2:

Just put a slash in front of your path. So instead of

<img src="images/logo.gif" />

Try

<img src="/images/logo.gif" />

Without the slash it will append the path to the relative path as you discovered. Hammad solution should work too since it will generate the right code but now you know why you had problems.