[英]symfony2 fatal error Cannot redeclare class
Ok, I've been at this for two hours now and I see some other people have had this error, but I can't seem to match their causes/resolutions with mine. 好吧,我现在已经在这两个小时了,我看到其他人有这个错误,但我似乎无法与他的原因/决议相匹配。
Fatal error: require() [function.require]: Cannot redeclare class companycontroller in /var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php on line 55 致命错误:require()[function.require]:无法在第55行的/var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php中重新声明类companycontroller
The terminal gives a better error message pointing me to the end clause of the actual class that it reports having trouble with (trying to redeclare). 终端提供了一个更好的错误消息,指向我报告的实际类的结束子句(尝试重新声明)。
If I remove or rename the file companyController.php it throws a Symfony2 error saying that the it went looking for the class but didn't find it where it was expected. 如果我删除或重命名文件companyController.php它会抛出一个Symfony2错误,说它正在查找该类,但没有找到它在预期的位置。
If I put the file back in its place, apache throws a php error saying that the class companyController can't be redeclared. 如果我把文件放回原位,apache抛出一个php错误,说不能重新声明类companyController。
I only have it once?! 我只有一次吗?!
Here is the entire class... if anyone has patience to try and help me out... 这是整个班级...如果有人有耐心尝试帮助我...
<?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()
;
}
}
So, turns out that was a clumpsy typo by moi there. 事实证明,这是一个由moi在那里发生的一堆拼写错误。
But for anyone else who runs into this error message in Symfony2: 但对于在Symfony2中遇到此错误消息的任何其他人:
Fatal error: require() [function.require]: Cannot redeclare class... 致命错误:require()[function.require]:无法重新声明类...
Here is a hint: check if you have accidentally deleted or typo:ed the namespace in the file that contains the definition of the class that php claims it is trying to re-define. 这是一个提示:检查是否有意外删除或错误:在包含php声称它试图重新定义的类的定义的文件中的命名空间。
The php error message doesn't really give you a clue to look for that... =) PHP错误消息并没有真正给你一个线索寻找... =)
redeclare class - 可能有两个同名的课程
Sometimes, if you got seduced by copy/paste, check your classnames, namespaces and for other "typos" that could have happened. 有时,如果您被复制/粘贴诱惑,请检查您的类名,命名空间以及可能发生的其他“拼写错误”。 (copy/paste is the devil of programming :/)
(复制/粘贴是编程的魔鬼:/)
Similar to other answers, in my case I had renamed the class but not the containing file. 与其他答案类似,在我的情况下,我已经重命名了类,但没有重命名包含文件。 Every class should be declared in a file with the same name.
应该在具有相同名称的文件中声明每个类。 So check that, too.
所以检查一下。
In my case, it was a use
statement under the namespace which used the same classname (but another path). 在我的例子中,它是命名空间下的一个
use
语句,它使用相同的类名(但是另一个路径)。
namespace Bsz\RecordTab;
use \Bsz\Config\Libraries; // I used this in constructor
class Libraries
{
...
}
Without the use directive, it Worked 没有使用指令,它工作
Personally, I juste removed cache manually and it worked 就个人而言,我juste手动删除缓存,它工作
rm -rf app/cache/*
Clearing cache was not fixing my problem. 清除缓存并没有解决我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.