php symfony2 致命错误无法重新声明类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11776145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
symfony2 fatal error Cannot redeclare class
提问by Matt Welander
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()
;
}
}
回答by Matt Welander
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 错误消息并没有真正为您提供寻找该信息的线索... =)
回答by ReaperSoon
Personally, I juste removed cache manually and it worked
就个人而言,我只是手动删除缓存并且它有效
rm -rf app/cache/*
Clearing cache was not fixing my problem.
清除缓存并没有解决我的问题。
回答by Eran Or
redeclare class - Likely there is tow classes with the same name
重新声明类 - 可能有两个同名的类
回答by Michael Kargl
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 :/)
有时,如果您被复制/粘贴所吸引,请检查您的类名、命名空间以及其他可能发生的“错别字”。(复制/粘贴是编程的魔鬼:/)
回答by Arild
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.
与其他答案类似,在我的情况下,我重命名了类,但没有重命名包含文件。每个类都应该在同名的文件中声明。所以也检查一下。
回答by Corni
In my case, it was a usestatement 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
没有使用指令,它有效

