在 Zend Framework 中显示图像 [英] Displaying image in Zend Framework

查看:18
本文介绍了在 Zend Framework 中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Zend Framework 的新手,所以如果这是一个基本问题,请原谅我,但我很忙,显示从数据库中检索到的图像.

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.

我基本上无法访问/public/文件夹中的任何资源.我猜前端控制器正在重定向 标签和 标签内的 src 请求.每次我搜索时,我都会看到人们在访问/public/文件夹之外的图像时遇到问题……但只要它们存储在/public 文件夹下,它们似乎就可以了.我做错了什么?

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>

$user 进行转储会得到以下结果:

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.

推荐答案

从技术上讲,如果您使用框架,则不应在视图中执行逻辑.您应该在控制器和视图中执行 if/else 语句:

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>

但至于你的问题,如果你在浏览器中复制粘贴 src 属性,它会显示图片吗?

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

这是我的.htaccess.它直接提供扩展名与此正则表达式匹配的任何文件,并将其他所有内容重定向到我的 index.php,这是我的引导文件.

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

这篇关于在 Zend Framework 中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆