Symfony 3.4无法在生产环境中删除

时间:2018-06-13 13:11:44

标签: symfony delete-method

我有一个我无法解决的错误。 我有一个Symfony应用程序,在我的本地和登台服务器上的dev env中运行良好,所以使用app_dev.php。 当我在prod mod中测试我的登台服务器上的同一个应用程序时(所以使用app.php或只是http://servername/删除表单不起作用。我的日志中有错误:Request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: "No route found for "POST /fr/invitesgroupe/3024": Method Not Allowed (Allow: DELETE)" < / p>

我用CRUD生成器生成了这个部分: 控制器:

namespace AppBundle\Controller;

use AppBundle\Entity\InvitesGroupe;
use AppBundle\Entity\Reservations;
use AppBundle\Form\InvitesGroupeType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * InvitesGroupe controller.
 *
 * @Route("/invitesgroupe")
 */
class InvitesGroupeController extends Controller
{
    /**
     * Lists all InvitesGroupe entities.
     *
     * @Route("/", name="invitesgroupe_index")
     * @Method("GET")
     */
    public function indexAction(Request $request)
    {

        $em = $this->getDoctrine()->getManager();

        $day = $request->get('day');
        $resto = $request->get('resto');

        $user = !$this->get('security.authorization_checker')
            ->isGranted('ROLE_SUPERVISEUR') ? "AND ig.user = '".$this->getUser()->getId()."'" : "";

        if ($day) {
            $day = \DateTime::createFromFormat('d/m/Y', $day);
            $dql   = "SELECT ig FROM AppBundle:InvitesGroupe ig 
                    LEFT JOIN AppBundle:Reservations r WITH r.idInvite = ig.idInvite
                    WHERE ig.inviteVisible = 1
                    AND ig.inviteTemporaire IS NULL
                    AND r.date = '".$day->format('Y-m-d')."'
                    ".$user."
                    ORDER BY ig.idInvite DESC";
        } elseif ($resto && $day) {
            $day = \DateTime::createFromFormat('d/m/Y', $day);
            $dql   = "SELECT ig FROM AppBundle:InvitesGroupe ig 
                    LEFT JOIN AppBundle:Reservations r WITH r.idInvite = ig.idInvite
                    WHERE ig.inviteVisible = 1 AND ig.inviteTemporaire IS NULL
                    AND r.idRestaurant = ".$resto."
                    AND r.date = '".$day->format('Y-m-d')."'
                    ".$user."
                    ORDER BY ig.idInvite DESC";

        } else {
            $dql   = "SELECT ig FROM AppBundle:InvitesGroupe ig
                    LEFT JOIN AppBundle:Reservations r WITH r.idInvite = ig.idInvite
                    WHERE ig.inviteVisible = 1 AND ig.inviteTemporaire IS NULL
                    AND r.date >= CURRENT_TIMESTAMP()
                    ".$user."
                    ORDER BY ig.idInvite DESC";
        }

        $query = $em->createQuery($dql);


        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $query, /* query NOT result */
            $request->query->getInt('page', 1)/*page number*/,
            10/*limit per page*/
        );

        $deleteForms = array();

        if (null !== $request->get('print')) {
            return $pagination;
        }

        foreach ($pagination as $page) {
            $deleteForms[$page->getIdInvite()] = $this->createDeleteForm($page)->createView();
        }

        if ($day) {
            $restoTest = array();
            $restos = array();
            foreach ($pagination as $invite) {
                if (!in_array($invite->getIdService()->getIdRestaurant()->getIdRestaurant(), $restoTest)) {
                    $obj = new \stdClass();
                    $obj->id = $invite->getIdService()->getIdRestaurant()->getIdRestaurant();
                    $obj->nom = $invite->getIdService()->getIdRestaurant()->getNomRestaurant();
                    $restos[] = $obj;
                    $restoTest[] = $invite->getIdService()->getIdRestaurant()->getIdRestaurant();

                }
            }
        }



        return $this->render('invitesgroupe/index.html.twig', array(
            'invitesGroupes' =>  $pagination,
            'delete_forms' => $deleteForms,
            'day'  => $day ? $day : null,
            'restos' => isset($restos) ? json_encode($restos) : null
        ));
    }

    /**
     * Creates a new InvitesGroupe entity.
     *
     * @Route("/new", name="invitesgroupe_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $invitesGroupe = new InvitesGroupe();
        $reservation = new Reservations();
        $invitesGroupe->addReservation($reservation);
        $invitesGroupe->setUser($this->getUser());
        $form = $this->createForm('AppBundle\Form\InvitesGroupeType', $invitesGroupe);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
//            $coutRepas = $em->getRepository('AppBundle:CoutsRepas')->coutParDefaut();
            // récupération des dates dans datepicker
            $datePicker = $form['datepicker']->getData();
            $datePicker = explode(',', $datePicker);

            $dates = array();

            foreach ($datePicker as $dt) {
                $dates[] = \DateTime::createFromFormat('d/m/Y', $dt);
            }

            $first = true;
            /*
             * First reservation is in the form
             */

            if (!$form->getViewData()->getReservations()[0]->getIdService()) {
                $service = $em->getRepository('AppBundle:Services')
                    ->findOneBy(
                        array(
                            'idRestaurant' => $form->getViewData()->getReservations()[0]->getIdRestaurant(),
                            'serviceResa' => 1
                        )
                    );
                $form->getViewData()->getReservations()[0]->setIdService($service);
            } else {
                $service = $form->getViewData()->getReservations()[0]->getIdService();
            }

            $invitesGroupe->setIdService($service);
            if (null === $form->getViewData()->getUser()) {
                $invitesGroupe->setUser($this->getUser());
            }

            foreach ($dates as $date) {
                if ($first) {
                    $reservation = $form->getViewData()->getReservations()[0];

                    $invitesGroupe->setNombre($reservation->getNbPlace());

                    $reservation->setDate($date)
                        ->setIdRestaurant($form->getViewData()->getReservations()[0]->getIdRestaurant())
                        ->setIdService($service)
                        ->setNbPlace($invitesGroupe->getNombre())
//                        ->setCoutTotal($reservation->getNbPlace() * $coutRepas->getCout())
                    ;

                    $invitesGroupe->setNombre($reservation->getNbPlace());
                    $first = false;
                } else {
                    $reservation = new Reservations();
                    $reservation->setDate($date)
                        ->setIdRestaurant($form->getViewData()->getReservations()[0]->getIdRestaurant())
                        ->setIdService($service)
                        ->setNbPlace($invitesGroupe->getNombre())
//                        ->setCoutTotal($reservation->getNbPlace() * $coutRepas->getCout())
                        ->setRepasCompris($reservation->getRepasCompris())
                    ;
                }

                $invitesGroupe->addReservation($reservation);
                $reservation->setInvitesGroupes($invitesGroupe);

                $em->persist($reservation);
                // $em->flush();


            }

            $em->persist($invitesGroupe);
            $em->flush();


            // sending confirmation mail

            $message = \Swift_Message::newInstance()
                ->setSubject('['.$form->getViewData()
                        ->getReservations()[0]->getIdRestaurant()
                        ->getNomRestaurant().'] Réservation prise en compte')
                ->setFrom('')
                ->setTo($invitesGroupe->getUser()->getEmail())
                ->setBody(
                    $this->renderView(
                    // app/Resources/views/emails/invitegroupe.html.twig
                        'emails/invitegroupe.html.twig',
                        array('invite' => $invitesGroupe,
                            'modif' => false,
                            'temp' => false)
                    ),
                    'text/html'
                )
            ;
            $this->get('mailer')->send($message);

            return $this->redirectToRoute('invitesgroupe_index');
        }

        return $this->render('invitesgroupe/new.html.twig', array(
            'invitesGroupe' => $invitesGroupe,
            'form' => $form->createView(),
        ));
    }


    /**
     * Print pdf of the day selected.
     *
     * @Route("/print", name="invitesgroupe_print")
     * @Method("GET")
     */
    public function printAction(Request $request)
    {

        $invites = $this->indexAction($request);

        $services = array();
        foreach ($invites as $invite) {
            if (!in_array($invite->getIdService(), $services) && $invite->getIdService()
                    ->getIdRestaurant()->getIdRestaurant() == $request->get('resto')) {
                $services[] = $invite->getIdService();
            }
        }

        $day = \DateTime::createFromFormat('d/m/Y', $request->get('day'));

        // on stocke la vue à convertir en PDF,
        // en n'oubliant pas les paramètres twig si la vue comporte des données dynamiques
        $html = $this->render('pdf/invitation.html.twig', array('invites' => $invites,
            'day' => $day,
            'resto' => $request->get('resto'),
            'services' => $services
        ));
        //on appelle le service html2pdf
        $html2pdf = $this->get('html2pdf_factory')->create();
        //real : utilise la taille réelle
        $html2pdf->pdf->SetDisplayMode('real');
        // $html2pdf->setModeDebug();
        //writeHTML va tout simplement prendre la vue stocker dans la variable $html pour la convertir en format PDF
        $html2pdf->writeHTML($html);
        //Output envoit le document PDF au navigateur internet
        return new Response($html2pdf->Output('invitation-du'.$day->format('d-m-Y').'.pdf'),
            200, array('Content-Type' => 'application/pdf')
        );
    }

    /**
     * Print pdf of the day selected.
     *
     * @Route("/see", name="invitesgroupe_see")
     * @Method("GET")
     */
    public function seeAction(Request $request) {

        $invites = $this->indexAction($request);

        $services = array();
        foreach ($invites as $invite) {
            if (!in_array($invite->getIdService(), $services) && $invite->getIdService()
                    ->getIdRestaurant()->getIdRestaurant() == $request->get('resto')) {
                $services[] = $invite->getIdService();
            }
        }

        $day = \DateTime::createFromFormat('d/m/Y', $request->get('day'));

        return $this->render('pdf/invitation.html.twig', array('invites' => $invites,
            'day' => $day,
            'resto' => $request->get('resto'),
            'services' => $services
        ));
    }

    /**
     * Displays a form to edit an existing InvitesGroupe entity.
     *
     * @Route("/{id}/edit", name="invitesgroupe_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, InvitesGroupe $invitesGroupe)
    {
        $deleteForm = $this->createDeleteForm($invitesGroupe);
        $editForm = $this->createForm('AppBundle\Form\InvitesGroupeType', $invitesGroupe);
        $editForm->handleRequest($request);
        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $em = $this->getDoctrine()->getManager();
            /**
             * @Doc : retrieve dates from field datepicker
             */
            $datePicker = $editForm['datepicker']->getData();
            $datePicker = explode(',', $datePicker);
            $dates = array();
            foreach ($datePicker as $dt) {
                $dates[] = \DateTime::createFromFormat('d/m/Y', $dt);
            }

            $data = $editForm->getViewData();
            $postedReservation = $data->getReservations()[0];

            // changement de restaurant et de service
            $restaurant = $postedReservation->getIdRestaurant() !== null ?
                $postedReservation->getIdRestaurant() :
                $invitesGroupe->getIdService()->getIdRestaurant();

            if ($postedReservation->getIdService() !== $invitesGroupe->getIdService()) {
                $invitesGroupe->setIdService($postedReservation->getIdService());
            }

//            $coutRepas = $em->getRepository('AppBundle:CoutsRepas')->coutParDefaut();

            /**
             * @doc : check if one date has been removed
             */

            $invitesGroupe->setNombre($data->getReservations()[0]->getNbPlace());

            $checkDates = $reservation= $em->getRepository('AppBundle:Reservations')
                ->findByIdInvite($invitesGroupe->getIdInvite());

            foreach ($checkDates as $checkDate) {
                if (!in_array($checkDate, $dates)) {
                    // date removed
                    $invitesGroupe->removeReservation($checkDate);
                    $em->remove($checkDate);
                    $em->persist($invitesGroupe);
                    $em->flush();
                }
            }

            if (null === $editForm->getViewData()->getUser()) {
                $invitesGroupe->setUser($this->getUser());
            }

            foreach ($dates as $date) {

                /**
                 * @doc : dates is new ?
                 */
                $reservation= $em->getRepository('AppBundle:Reservations')
                    ->findBy(array('idInvite' => $invitesGroupe->getIdInvite(), 'date' => $date));

                if (!$reservation) {
                    /* yes */
                    $reservation = new Reservations();
                    $reservation->setDate($date)
                        ->setIdRestaurant($restaurant)
                        ->setIdService($invitesGroupe->getIdService())
                        ->setNbPlace($invitesGroupe->getNombre())
//                        ->setCoutTotal($invitesGroupe->getNombre() * $coutRepas->getCout())
                        ->setRepasCompris($invitesGroupe->getRepasOffert())
                    ;

                    $invitesGroupe->addReservation($reservation);
                    $reservation->setInvitesGroupes($invitesGroupe);

                    $em->persist($reservation);

                }

            }

            $em->persist($invitesGroupe);
            $em->flush();

            // sending confirmation mail

            $message = \Swift_Message::newInstance()
                ->setSubject('['.$restaurant->getNomRestaurant().'] Réservation modifiée')
                ->setFrom('')
                ->setTo($invitesGroupe->getUser()->getEmail())
                ->setBody(
                    $this->renderView(
                    // app/Resources/views/emails/invitegroupe.html.twig
                        'emails/invitegroupe.html.twig',
                        array('invite' => $invitesGroupe,
                            'restaurant' => $restaurant,
                            'modif' => true,
                            'temp' => false)
                    ),
                    'text/html'
                )
            ;
            $this->get('mailer')->send($message);

            return $this->redirectToRoute('invitesgroupe_index');
        }

        return $this->render('invitesgroupe/edit.html.twig', array(
            'invitesGroupe' => $invitesGroupe,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a InvitesGroupe entity.
     *
     * @Route("/{id}", name="invitesgroupe_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, InvitesGroupe $invitesGroupe)
    {
        $form = $this->createDeleteForm($invitesGroupe);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid() &&
            $this->get('security.authorization_checker')->isGranted('ROLE_SUPERVISEUR')
        ) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($invitesGroupe);
            $em->flush();

            // sending confirmation mail

            if ($invitesGroupe->getUser() !== $this->getUser()) {
                $restaurant = $em->getRepository('AppBundle:Restaurants')
                    ->findOneByIdRestaurant($invitesGroupe->getIdService()->getIdRestaurant());

                $message = \Swift_Message::newInstance()
                    ->setSubject('['.$restaurant->getNomRestaurant().'] Réservation annulée')
                    ->setFrom('')
                    ->setPriority(1)
                    ->setTo($invitesGroupe->getUser()->getEmail())
                    ->setBody(
                        $this->renderView(
                        // app/Resources/views/emails/invitegroupe_canceled.html.twig
                            'emails/invitegroupe.html.twig',
                            array('invite' => $invitesGroupe,
                                'restaurant' => $restaurant,
                                'delete' => true,
                                'temp' => false,
                                'modif' => true
                            )
                        ),
                        'text/html'
                    )
                ;
                $this->get('mailer')->send($message);
            }

        }

        return $this->redirectToRoute('invitesgroupe_index');
    }

    /**
     * Creates a form to delete a InvitesGroupe entity.
     *
     * @param InvitesGroupe $invitesGroupe The InvitesGroupe entity
     *
     * @return mixed
     */
    private function createDeleteForm(InvitesGroupe $invitesGroupe)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('invitesgroupe_delete', array('id' => $invitesGroupe->getIdInvite())))
            ->setMethod('DELETE')
            ->getForm()
            ;
    }
}

我在plesk Onyx 17.8.11,php 5.6.36上。 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

您的请求方法是POST,但是在您的路由声明后,“/ fr / invitesgroupe / 3024”由deleteAction()处理,并且唯一允许的方法是“DELETE”。所以 “POST / fr / invitesgroupe / 3024”应为“DELETE / fr / invitesgroupe / 3024”。