symfony2致命错误无法重新声明类

时间:2012-08-02 11:05:34

标签: php symfony fatal-error

好的,我已经在这里待了两个小时了,我看到其他人有这个错误,但我似乎无法与他的原因/决议相符。

致命错误:require()[function.require]:无法在第55行的/var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php中重新声明类companycontroller

终端提供了一个更好的错误消息,指出我报告遇到问题的实际类的结束子句(尝试重新声明)。

如果我删除或重命名文件companyController.php,它会抛出一个Symfony2错误,说它正在查找该类,但没有找到它在预期的位置。

如果我把文件放回原位,apache会抛出一个php错误,说不能重新声明类companyController。

我只有一次?!

以下是整个课程......如果有人有耐心尝试帮助我......

<?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()
        ;
    }
}

6 个答案:

答案 0 :(得分:54)

所以,事实证明这是一个由moi在那里发生的一堆错字。

但是对于在Symfony2中遇到此错误消息的任何人:

致命错误:require()[function.require]:无法重新声明类...

这是一个提示:检查你是否有意外删除或拼写错误:在包含php声称它试图重新定义的类的定义的文件中的命名空间。

php错误消息并没有真正为您提供寻找的信息... =)

答案 1 :(得分:0)

redeclare class - 可能有两个同名的课程

答案 2 :(得分:0)

有时,如果您被复制/粘贴诱惑,请检查您的类名,命名空间以及可能发生的其他“拼写错误”。 (复制/粘贴是编程的魔鬼:/)

答案 3 :(得分:0)

与其他答案类似,在我的情况下,我已经重命名了类,但没有重命名包含文件。应该在具有相同名称的文件中声明每个类。所以也要检查一下。

答案 4 :(得分:0)

就我而言,它是命名空间下的use语句,它使用相同的类名(但是另一个路径)。

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

没有use指令,它工作

答案 5 :(得分:0)

就我个人而言,我手动删除了缓存并且工作正常

rm -rf app/cache/*

清除缓存并没有解决我的问题。