Displaying image in Zend Framework

依然范特西╮ 提交于 2019-12-11 19:49:04

问题


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 'test@google.com' (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:


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


来源:https://stackoverflow.com/questions/10441516/displaying-image-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!