douxi2670 2019-02-21 07:19
浏览 91
已采纳

在null上调用成员函数guessExtension()

it gives the error that i wrote on the title. I've used this exact same code before but i doesnt work now. I think i doesnt get the "id" but i dont know why. if i uncomment those comment lines on the controller it opens the photo add window and lets me select a photo but after clicking submit, it doesnt upload. please guys i really need your help. any ideas? I've seen same erorr on stackoverflow here but that solution didnt work for me

*****This is my controller*****

/**
     * @Route("/{id}/fotoekle", name="duyurular_fotoekle", methods={"GET","POST"})
     */
    public function fotoekle(Request $request, $id, Duyurular $duyurular): Response
    {
        $form = $this->createForm(DuyurularType::class, $duyurular);
        $form->handleRequest($request);
        echo ('sefa');
       // if ($form->isSubmitted() && $form->isValid()) {
            echo ('sefa2');
            $file = $request->files->get('image');
            $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
  //          dump($request);
    //        die();
            // $file = $duyurular->getFotograf();
            // $file = $form->get('fotograf')->getData();

            try{
                $file->move(
                    $this->getParameter('fotograflar_directory'),
                    $fileName
                );
            }catch (FileException $e){
            }
            $duyurular->setFotograf($fileName);
            $this->getDoctrine()->getManager()->flush();
            return $this->redirectToRoute('duyurular_fotoekle', ['id' => $duyurular->getId(),]);
//        }
//        return $this->render('duyurular/fotoekle.html.twig', [
//            'duyurular' => $duyurular,
//            'form' => $form->createView(),
//            'id' => $duyurular->getId(),
//        ]);
    }

This is my .html.twig file/ form code

{% block body %}
    <h1>{{ duyurular.baslik }}</h1>
    <form action="{{ path('duyurular_fotoekle',{'id':id}) }}" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input name="duyuruid" value="{{ id }}" type="number" readonly>
        <input type="file" name="image" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    {% if duyurular.fotograf != null %}
       <img src="{{ asset('yuklenen') }}/fotograflar/{{ duyurular.fotograf }}" height="100">
    {% endif %}
{% endblock %}
  • 写回答

1条回答 默认 最新

  • doukang7486 2019-02-21 07:58
    关注

    I had to make a change on the controller. I added another function called 'iedit' than in the iedit.html.twig i called 'duyurular_fotoekle' . if you couldnt understand what i did dont hesitate to ask.

    /**
         * @Route("/{id}/fotoekle", name="duyurular_fotoekle", methods={"POST"})
         */
        public function fotoekle(Request $request, Duyurular $duyurular, $id): \Symfony\Component\HttpFoundation\RedirectResponse
        {
            $form = $this->createForm(DuyurularType::class, $duyurular);
            $form->handleRequest($request);
            echo ('sefa 2');
            $file = $request->files->get('image');
            // $file = $duyurular->getFotograf();
            // $file = $form->get('fotograf')->getData();
            $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
            try{
                $file->move(
                    $this->getParameter('fotograflar_directory'),
                    $fileName
                );
            }catch (FileException $e){
            }
            $duyurular->setFotograf($fileName);
            $this->getDoctrine()->getManager()->flush();
            return $this->redirectToRoute('duyurular_iedit', ['id' => $duyurular->getId(),]);
        }
    
        /**
         * @Route("/{id}/iedit", name="duyurular_iedit", methods="GET|POST")
         */
        public function iedit(Request $request,$id, Duyurular $duyurular,DuyurularRepository $duyurularRepository): Response
        {
            $duyurulist = $duyurularRepository ->findAll();
            $duyuruname = $duyurularRepository ->findBy(['id'=> $duyurular->getId()]);
            $form = $this->createForm(DuyurularType::class, $duyurular);
            $form->handleRequest($request);
    
            echo ('sefa 0');
            $this->getDoctrine()->getManager()->flush();
            if ($form->isSubmitted()) {
    
                echo ('sefa 1');
                return $this->redirectToRoute('duyurular_index', ['id' => $duyurular->getId()]);
            }
    
            return $this->render('duyurular/iedit.html.twig', [
                'duyurular' => $duyurular,
                'id'=>$id,
                'duyurulist' => $duyurulist,
                'duyuruname' => $duyuruname,
                'form' => $form->createView(),
            ]);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计