2012-08-02 18 views
17

Ok, sono stato a questo per due ore e vedo altre persone hanno avuto questo errore, ma non riesco a trovare corrispondenza con le loro cause/risoluzioni con il mio.symfony2 Errore irreversibile Impossibile redeclare la classe

Fatal error: require() [function.require]: Impossibile ridichiarare classe companycontroller in /var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php on line 55

Il terminale fornisce un messaggio di errore migliore indicandomi la clausola finale della classe effettiva che segnala di aver avuto problemi con (cercando di ridichiarare).

Se rimuovo o rinomina il file companyController.php, genera un errore di Symfony2 che dice che è andato alla ricerca della classe ma non è stato trovato dove era previsto.

Se rimetto il file al suo posto, apache genera un errore php dicendo che la classe companyController non può essere dichiarata.

L'ho avuto solo una volta ?!

Qui è l'intera classe ... se qualcuno ha la pazienza per cercare di darmi una mano ...

<?php 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

use BizTV\BackendBundle\Entity\company; 
use BizTV\BackendBundle\Form\companyType; 

/** 
* company controller 
* 
*/ 

class companyController extends Controller 
{ 
    /** 
    * Lists all company entities. 
    * 
    */ 
    public function indexAction() 
    { 
     $em = $this->getDoctrine()->getEntityManager(); 

     $entities = $em->getRepository('BizTVBackendBundle:company')->findAll(); 

     return $this->render('BizTVBackendBundle:company:index.html.twig', array(
      'entities' => $entities 
     )); 
    } 

    /** 
    * Finds and displays a company entity. 
    * 
    */ 
    public function showAction($id) 
    { 
     $em = $this->getDoctrine()->getEntityManager(); 

     $entity = $em->getRepository('BizTVBackendBundle:company')->find($id); 

     if (!$entity) { 
      throw $this->createNotFoundException('Unable to find company entity.'); 
     } 

     $deleteForm = $this->createDeleteForm($id); 

     return $this->render('BizTVBackendBundle:company:show.html.twig', array(
      'entity'  => $entity, 
      'delete_form' => $deleteForm->createView(), 

     )); 
    } 

    /** 
    * Displays a form to create a new company entity. 
    * 
    */ 
    public function newAction() 
    { 
     $entity = new company(); 
     $form = $this->createForm(new companyType(), $entity); 

     return $this->render('BizTVBackendBundle:company:new.html.twig', array(
      'entity' => $entity, 
      'form' => $form->createView() 
     )); 
    } 

    /** 
    * Creates a new company entity. 
    * 
    */ 
    public function createAction() 
    { 
     $entity = new company(); 
     $request = $this->getRequest(); 
     $form = $this->createForm(new companyType(), $entity); 
     $form->bindRequest($request); 

     if ($form->isValid()) { 
      $em = $this->getDoctrine()->getEntityManager(); 
      $em->persist($entity); 
      $em->flush(); 

      /* Create adminuser for this company to go along with it */ 
      $userManager = $this->container->get('fos_user.user_manager'); 
      $user = $userManager->createUser(); 

      //make password (same as username) 
      $encoder = $this->container->get('security.encoder_factory')->getEncoder($user); //get encoder for hashing pwd later 
      $tempPassword = $entity->getCompanyName(); //set password to equal company name 

      //Get company 
      $tempCompanyId = $entity->getId(); //get the id of the just-inserted company (so that we can retrieve that company object below for relating it to the user object later) 
      $tempCompany = $em->getRepository('BizTVBackendBundle:company')->find($tempCompanyId); //get the company object that this admin-user will belong to 

      $user->setUsername($entity->getCompanyName() . "/admin"); //set username to $company/admin 
      $user->setEmail('admin.'.$entity->getCompanyName().'@example.com'); //set email to non-functioning (@example) 
      $user->setPassword($encoder->encodePassword($tempPassword, $user->getSalt())); //set password with hash 
      $user->setCompany($tempCompany); //set company for this user    
      $user->setConfirmationToken(null); //we don't need email confirmation of account 
      $user->setEnabled(true); //without a confirmation token, we of course also need to flag the account as enabled manually 
      $user->addRole('ROLE_ADMIN'); 

      $userManager->updateUser($user); 

      return $this->redirect($this->generateUrl('company_show', array('id' => $entity->getId()))); 

     } 

     return $this->render('BizTVBackendBundle:company:new.html.twig', array(
      'entity' => $entity, 
      'form' => $form->createView() 
     )); 
    } 

    /** 
    * Displays a form to edit an existing company entity. 
    * 
    */ 
    public function editAction($id) 
    { 
     $em = $this->getDoctrine()->getEntityManager(); 

     $entity = $em->getRepository('BizTVBackendBundle:company')->find($id); 

     if (!$entity) { 
      throw $this->createNotFoundException('Unable to find company entity.'); 
     } 

     $editForm = $this->createForm(new companyType(), $entity); 
     $deleteForm = $this->createDeleteForm($id); 

     return $this->render('BizTVBackendBundle:company:edit.html.twig', array(
      'entity'  => $entity, 
      'edit_form' => $editForm->createView(), 
      'delete_form' => $deleteForm->createView(), 
     )); 
    } 

    /** 
    * Edits an existing company entity. 
    * 
    */ 
    public function updateAction($id) 
    { 
     $em = $this->getDoctrine()->getEntityManager(); 

     $entity = $em->getRepository('BizTVBackendBundle:company')->find($id); 

     if (!$entity) { 
      throw $this->createNotFoundException('Unable to find company entity.'); 
     } 

     $editForm = $this->createForm(new companyType(), $entity); 
     $deleteForm = $this->createDeleteForm($id); 

     $request = $this->getRequest(); 

     $editForm->bindRequest($request); 

     if ($editForm->isValid()) { 
      $em->persist($entity); 
      $em->flush(); 

      return $this->redirect($this->generateUrl('company_edit', array('id' => $id))); 
     } 

     return $this->render('BizTVBackendBundle:company:edit.html.twig', array(
      'entity'  => $entity, 
      'edit_form' => $editForm->createView(), 
      'delete_form' => $deleteForm->createView(), 
     )); 
    } 

    /** 
    * Deletes a company entity. 
    * 
    */ 
    public function deleteAction($id) 
    { 
     $form = $this->createDeleteForm($id); 
     $request = $this->getRequest(); 

     $form->bindRequest($request); 

     if ($form->isValid()) { 
      $em = $this->getDoctrine()->getEntityManager(); 
      $entity = $em->getRepository('BizTVBackendBundle:company')->find($id); 

      if (!$entity) { 
       throw $this->createNotFoundException('Unable to find company entity.'); 
      } 

      $em->remove($entity); 
      $em->flush(); 
     } 

     return $this->redirect($this->generateUrl('company')); 
    } 

    private function createDeleteForm($id) 
    { 
     return $this->createFormBuilder(array('id' => $id)) 
      ->add('id', 'hidden') 
      ->getForm() 
     ; 
    } 
} 
+1

Hai provato a grep per 'companyController'? –

+2

non hai spazio dei nomi definito nel controller. Forse potrebbe essere quello? – unairoldan

+0

Grazie. Ho appena rigenerato la crude per l'entità e questo è quello che ho scoperto anche io - ieri sera ho aggiunto un commento alla parte superiore del documento - deve aver accidentalmente evidenziato la linea dello spazio dei nomi appena ho iniziato a digitare il mio commento, sostituendo così lo spazio dei nomi con un commento ... Non è possibile contrassegnare questo tuo commento come la risposta alla discussione? –

risposta

52

Così, risulta che è stato un errore di battitura clumpsy da moi lì.

Ma per chiunque altro che si imbatte in questo messaggio di errore in Symfony2:

Fatal error: require() [function.require]: Impossibile ridichiarare classe ...

Ecco un suggerimento: controllare se hai cancellato o digitato accidentalmente: ed il namespace nel file che contiene la definizione della classe che php afferma che sta tentando di ridefinire.

Il messaggio di errore di PHP in realtà non vi darà un indizio per cercare che ... =)

+0

Sì, succede ...: D –

+0

Contrassegna questa risposta come accettata, quindi questa domanda non sarà filtrata da 'Unanswered' –

+1

Grazie per avermi salvato dal mio cervello stanco. – mattalxndr

0

classe ridichiarare - Probabilmente c'è classi di traino con lo stesso nome

0

A volte, se hai sedotto da copia/incolla, controlla i tuoi nomi di classe, spazi dei nomi e per altri "refusi" che potrebbero essere accaduti. (copia/incolla è il diavolo della programmazione: /)

0

Simile ad altre risposte, nel mio caso avevo rinominato la classe ma non il file contenente. Ogni classe dovrebbe essere dichiarata in un file con lo stesso nome. Quindi controlla anche quello.

0

Nel mio caso, era un'istruzione use sotto lo spazio dei nomi che utilizzava lo stesso nome di classe (ma un altro percorso).

namespace Bsz\RecordTab; 
use \Bsz\Config\Libraries; // I used this in constructor 
class Libraries 
{ 
... 
} 

Senza la direttiva use, ha funzionato

Problemi correlati