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


symfony2 fatal error Cannot redeclare class

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

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

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

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

所以,事实证明这是moi在那里的一个拼写错误。

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

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

这里有一个提示:检查您是否意外删除或键入了文件中的名称空间,该文件包含php声称要重新定义的类的定义。

php错误消息并没有真正为您提供寻找它的线索…=)

就我个人而言,我只是手动删除了缓存,它在中工作

rm -rf app/cache/*

清除缓存并不能解决我的问题。

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

有时,如果你被复制/粘贴所吸引,请检查你的类名、命名空间和其他可能发生的"拼写错误"。(复制/粘贴是编程的魔鬼:/)

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

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

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

在没有使用指令的情况下,它工作于