2012-08-02 21 views
17

Ok, he estado en esto durante dos horas y veo que otras personas han tenido este error, pero parece que no puedo unir sus causas/resoluciones con las mías.symfony2 error grave No se puede redeclarar la clase

error fatal: require() [function.require]: No se puede redeclare companycontroller clase en /var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php en la línea 55

El terminal da un mejor mensaje de error que me señala la cláusula final de la clase real con la que informa que tiene problemas (intenta redeclarar).

Si elimino o cambio el nombre del archivo companyController.php arroja un error de Symfony2 diciendo que fue buscando la clase pero no la encontró donde se esperaba.

Si vuelvo a colocar el archivo en su lugar, apache arroja un error de php diciendo que la clase companyController no puede ser redeclarada.

Solo lo tengo una vez ?!

Aquí está toda la clase ... si alguien tiene la paciencia para tratar de ayudarme ...

<?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

¿Has probado a grep para 'companyController'? –

+2

no tiene espacio de nombre definido en su controlador. Tal vez podría ser eso? – unairoldan

+0

Gracias. Acabo de regenerar la basura para la entidad y eso es lo que descubrí también - anoche, agregué un comentario en la parte superior del documento - debí haber resaltado accidentalmente la línea del espacio de nombres justo cuando comencé a escribir mi comentario, reemplazando así el espacio de nombres con un comentario ... ¿No es posible marcar este comentario como la respuesta al hilo? –

Respuesta

52

Así, resulta que era un error tipográfico clumpsy por moi allí.

Sin embargo, para cualquier otra persona que se encuentra con este mensaje de error en Symfony2:

Fatal error: require() [function.require]: No se puede redeclare clase ...

Aquí tiene una pista: comprobar si borró accidentalmente o escribió: el espacio de nombres en el archivo que contiene la definición de la clase que php dice que intenta redefinir.

El mensaje de error php en realidad no le dará una pista para buscar que ... =)

+0

Sí, sucede ...: D –

+0

Marque esta respuesta como aceptada, por lo que esta pregunta no será filtrada por 'Sin respuesta' –

+1

Gracias por salvarme de mi cerebro cansado. – mattalxndr

0

clase redeclarar - es probable que haya clases de remolque con el mismo nombre

0

A veces, si tienes seducido por copiar/pegar, revisa tus nombres de clase, espacios de nombres y otros "errores tipográficos" que podrían haber sucedido. (copiar/pegar es el diablo de la programación: /)

0

Al igual que en otras respuestas, en mi caso, he cambiado el nombre de la clase pero no el archivo que contiene. Cada clase debe declararse en un archivo con el mismo nombre. Así que revisa eso, también.

0

En mi caso, era una declaración use en el espacio de nombres que usaba el mismo nombre de clase (pero otra ruta).

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

Sin la directiva use, funcionó

Cuestiones relacionadas