2012-08-02 91 views
17

好吧,我已經在這裏兩個小時了,而且我看到其他人有這個錯誤,但我似乎無法與我的原因/解決方案相匹配。symfony2致命錯誤無法重新聲明類

致命錯誤:需要()[function.require]:不能線55重新聲明類companycontroller在/var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php

該終端給出了一個更好的錯誤消息,指出我們遇到的實際類的最後一個子句(嘗試重新聲明)。

如果我刪除或重命名文件companyController.php,它會拋出一個Symfony2錯誤,指出它找到了類,但沒有找到它所期望的位置。

如果我把文件放回原位,apache會拋出一個php錯誤,指出classController無法重新聲明。

我只有一次?!

這裏是整個類......如果任何人有耐心,試圖幫助我......

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

您是否嘗試過到grep爲'companyController'? – 2012-08-02 11:11:36

+2

您沒有在控制器中定義名稱空間。也許可能是那個? – unairoldan 2012-08-02 11:13:34

+0

謝謝。我剛剛重新創建了這個實體,這也是我發現的 - 昨晚深夜,我在文檔的頂部添加了一條評論 - 當我開始輸入我的評論時,必定意外突出顯示了命名空間行,從而替換了命名空間與評論... 是不可能標記你的這個評論作爲線程的答案? – 2012-08-02 11:22:41

回答

52

所以,原來這是由MOI有一個錯字clumpsy。

但對於誰比誰跑成Symfony2的此錯誤消息:

致命錯誤:要求()[function.require]:不能重新聲明類...

這裏是一個暗示:檢查你意外地刪除了或錯字:編輯了文件中的名稱空間,該文件中包含php聲稱它正試圖重新定義的類的定義。

PHP的錯誤消息並沒有真正給你一個線索,以尋找那些... =)

+0

是的,它發生...:d – 2012-08-02 11:41:04

+0

標記爲接受,所以這個問題不會被'Unanswered' – 2012-08-02 11:48:37

+1

感謝從我疲倦的大腦救了我過濾這個答案。 – mattalxndr 2012-08-08 04:26:32

0

重新聲明類 - 可能的存在拖類具有相同的名稱

0

有時候,如果你有通過複製/粘貼引誘,檢查你的類名,命名空間和可能發生的其他「拼寫錯誤」。 (複製/粘貼是編程的魔鬼:/)

0

與其他答案類似,在我的情況下,我已經重命名類但不包含包含文件。每個類都應該在同名的文件中聲明。所以檢查一下。

0

在我的情況下,這是一個use語句下使用相同的類名(但另一個路徑)的命名空間。

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

不使用指令,它的工作

相關問題