symfony 图像路径只能在没有端口的情况下工作

symfony image path only working without port

使用上传器服务,我设法将文件传递到正确的目录 /var/www/html/test/tmp,但我的图像显示不正确。 在树枝中,我用以下方式调用图像:

<img src="{{asset('test/tmp/'~img)}}" alt="user image">

路径创建正确...

ip_server:port/test/tmp/imgfile.jpg

但当我尝试加载我的页面时只显示默认文本,奇怪的是,如果我在外部选项卡中打开路径并删除端口,则会显示正确的图像。有人知道这个问题吗?

我的控制器看起来像这样:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Users;
use AppBundle\Service\FileUploader;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;


class DefaultController extends Controller
{
 /**
     * @Route("/bearbeiten/{id}", name="edit")
     */
     public function editAction($id, Request $request, FileUploader $fileUploader){
         //Daten aus der Datenbank mit $id
          $listen = $this->getDoctrine()
                ->getRepository('AppBundle:Users')
                ->find($id);
          $img = $listen->getBild();

        //Formular wird erstellt
        $form = $this->createFormBuilder($listen)
                ->add('vorname', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('nachname', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('strasse', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('ort', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('plz', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('beschreibung', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('bild', FileType::class, array('required'=>false, 'label'=>'Bild (JPEG-Datei)', 'data_class'=>null))
                ->add('save', SubmitType::class, array('label'=>'Speichern', 'attr'=>array('class'=>'btn btn-primary')))
                ->add('home', SubmitType::class, array('label'=>'Zurück', 'attr'=>array('class'=>'btn btn-default')))
                ->getForm();

        $form->handleRequest($request);
        //Falls die Form valid ist....
        if($form->isSubmitted() && $form->get('save')->isClicked()){
            //Doctrine aktivieren
             $em=$this->getDoctrine()->getManager();
             $user = $em->getRepository(Users::class)->find($id);
             $file = $form['bild']->getData();
             $filename = $fileUploader->upload($file);
             $user->setBild($filename);
             $em->flush();
             return $this->redirectToRoute('homepage');
         }
         if ($form->get('home')->isClicked()){
                return $this->redirectToRoute('homepage');
            }
        return $this->render('main/edit.html.twig', array('listen'=>$listen, 'form'=>$form->createView(), 'img'=>$img));
     }
}

提前感谢您的帮助:)

您可以尝试配置资产:

config.yml

# app/config/config.yml
framework:
# ...
  assets:
      packages:
          uploaded_files:
              base_urls: 'http://ip_server:port/test/tmp/'

并在树枝中使用

<img src="{{ asset(img, 'uploaded_files') }}">

文档:https://symfony.com/doc/current/reference/configuration/framework.html#packages