19

好的,我已经在这两个小时了,我看到其他一些人有这个错误,但我似乎无法将他们的原因/解决方案与我的匹配。

致命错误:require() [function.require]: Cannot redeclare class companycontroller in /var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php 在第 55 行

终端给出了一个更好的错误消息,将我指向它报告有问题的实际类的结束子句(试图重新声明)。

如果我删除或重命名文件 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()
        ;
    }
}
4

6 回答 6

55

所以,原来那是 moi 那里的一个笨拙的错字。

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

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

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

php 错误消息并没有真正给你一个线索来寻找那个...... =)

于 2012-08-02T11:31:34.197 回答
1

就个人而言,我只是手动删除了缓存并且它有效

rm -rf app/cache/*

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

于 2018-04-11T10:26:45.233 回答
0

重新声明类 - 可能有两个具有相同名称的类

于 2012-11-19T21:16:59.793 回答
0

有时,如果您被复制/粘贴所吸引,请检查您的类名、命名空间以及可能发生的其他“错别字”。(复制/粘贴是编程的恶魔:/)

于 2013-01-11T17:00:26.440 回答
0

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

于 2014-11-10T12:26:12.680 回答
0

在我的例子中,它是use命名空间下的一个语句,它使用了相同的类名(但另一个路径)。

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

没有使用指令,它工作

于 2016-08-05T12:24:00.713 回答