0
votes

I'm new to Zend Framework, so forgive me if this is a basic question, but I'm having a heck of a time, displaying an image retrieved from the database.

Edit: I basically can't access any resource from the /public/ folder. I'm guessing the front controller is redirecting src requests inside <link> tags and <img> tags. Everytime I search, I see people having problems accessing images outside the /public/ folder... but they seem to be fine as long as they're stored under the /public folder. What am I doing wrong?

I've got the following code in a view partial:

<?php
$user = Zend_Layout::getMvcInstance()->getView()->user;

?>
<section class="one-fifth first sidebar_profile">
  <div class="one-fifth first about_img"><img src="<?php if($user->picture){echo '/public/img/users/small_thumbs/'.$user->picture;}else{echo "#";}  ?>" alt="name"/>
      <p class="name"><?php  if($user) {echo $user->username; } else { echo "Username";} ?></p>

Doing a dump of $user gives me the following:

object(Zend_Db_Table_Row)[70]
protected '_data' => 
array
  'userID' => string '22' (length=2)
  'username' => string 'test' (length=4)
  'firstName' => string '' (length=0)
  'lastName' => string '' (length=0)
  'email' => string '[email protected]' (length=18)
  'password' => string '098f6bcd4621d373cade4e832627b4f6' (length=32)
  'picture' => string '4fa3072fe09a5.jpg' (length=17)
  'verified' => string '1' (length=1)
  'confirm_key' => null
  'admin' => null

It goes on quite a while, but the point is the file is there. Looking at the source code from the page that's rendered, the line in question is rendered as:

<img alt="name" src="/public/img/users/small_thumbs4fa3072fe09a5.jpg">

Should I be using some sort of view helper to display this? I've searched and searched, and couldn't find one.

1
Are you using a virtual host? Typically, you would not have /public in your frontend urls; the public would be part of the virtual host definition.David Weinraub
yes I am. The /public/ folder is set as the doc root. I've tried it with/without the /public/ and it still won't work. Even when I give an absolute definition, I can't access images anywhere across the site. (Or css, unless I use @importBrian Vanderbusch
The standard .htaccess at the web root will bypass the framework for any files/directories that actually exist. If you enter http://yourhostname/img/users/small_thumbs4fa3072fe09a5.jpg into the browser, you don't get the image? Another thought: Is small_thumbs a folder? If so, you're missing a slash; the correct url would be /img/users/small_thumbs/4fa3072fe09a5.jpgDavid Weinraub
the missing slash was just a typo, I fixed that but had similar results. I spent a few hours looking into documentation, and wound up changing my .htaccess file, will post the final as an answer when I'm back on the work machine. The documentation says that the .htaccess file shouldn't redirect files in the same folder as the doc_root to the front controller, but despite that assurance, for some reason, my app was doing that anyways.Brian Vanderbusch

1 Answers

1
votes

Technically if you're using frameworks, you shouldn't be doing Logic in your View. You should do the if/else statement in your Controller and in your View simply do:

<div class="one-fifth first about_img"><img src="$img"/></div>

But as for your question goes, if you copy paste the src attribute in your browser, does it show the picture?

This is my .htaccess. It serves directly any file that extensions match this regular expression, and redirects everything else to my index.php which is my bootstrap file.

RewriteEngine On
RewriteRule \.(shtml|js|ico|gif|jpg|png|css|flv|swf|swz)$ - [NC,L]
RewriteRule ^.*$ index.php