php ClassNotFoundException: 试图加载类... Symfony
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20448767/
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
ClassNotFoundException: Attempted to load class... Symfony
提问by vazgen
I start to use Symfony about 5-7 days ago, if my question is very simple, sorry, but I can't find a solution of my problem. I've created 2 form classes - UserType and ClientType. Difference between them is that in ClientType form exists few additional fields. Here is Usertype class:
我大约 5-7 天前开始使用 Symfony,如果我的问题很简单,对不起,但我找不到解决我的问题的方法。我创建了 2 个表单类 - UserType 和 ClientType。它们之间的区别在于 ClientType 表单中存在很少的附加字段。这是用户类型类:
namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
$builder->add('username', 'text');
$builder->add('email', 'email', array(
'attr' => array('class'=>'email') ) );
$builder->add('Password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
));
$builder->add( 'roles', 'choice', array(
'choices' => array('ROLE_SYSTEM_ADMIN' => 'System Administrator', 'ROLE_ACCOUNT_ADMIN' => 'Account Manager', 'ROLE_UPLOADER' => 'Uploader'),
'mapped' => false ) );
$builder->add('is_active', 'checkbox', array(
'label' => 'Active (user can log in)',
'required' => false,
));
$builder->add('add_user', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Gyvfiles\GyvefileBundle\Entity\User'
));
}
public function getName()
{
return 'user';
}
}
And at last ClientType class. All the same, except this part: Update:
最后是 ClientType 类。都一样,除了这部分: 更新:
<?php
namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ClientType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text');
$builder->add('username', 'text');
$builder->add('email', 'email', array(
'attr' => array('class'=>'email') ) );
$builder->add('Password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
));
$builder->add('address', 'text');
$builder->add('telephone', 'text');
$builder->add('internalContactName', 'text');telephone
$builder->add( 'roles', 'checkbox', array(
'choices' => array('label' => 'Client can upload', ),
'required' => false ) );
$builder->add('is_active', 'checkbox', array(
'label' => 'Active (user can log in)',
'required' => false,
));
$builder->add('add_client', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Gyvfiles\GyvefileBundle\Entity\Client'
));
}
public function getName()
{
return 'client';
}
}
And after this steps I've used Usertype class in user controller successfully with this statement:
在此步骤之后,我已经通过以下语句成功地在用户控制器中使用了 Usertype 类:
$objUser = new User();
$objForm = $this->get( 'form.factory')->create( new UserType(), $objUser );
But when I tried to use ClientType in Client Controller with similar syntax:
但是当我尝试在客户端控制器中使用类似语法的 ClientType 时:
Update: ClientController
更新:ClientController
use Acme\Gyvfiles\GyvefileBundle\Entity\Permission;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Util\StringUtils;
use Acme\Gyvfiles\GyvefileBundle\Entity\Client;
use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType;
class ClientController extends Controller
{
public function addclientAction()
{
$em = $this->get( 'doctrine' )->getEntityManager();
$objClient = new Client();
$objForm = $this->get( 'form.factory')->create( new ClientType(), $objClient );
//$objForm->add('address', 'text');
return $this->render( 'AcmeGyvfilesGyvefileBundle:Client:addclient.html.twig', array(
'form' => $objForm->createView(),
));
}
}
appears Class not found exception.
出现 Class not found 异常。
"ClassNotFoundException: Attempted to load class "ClientType" from namespace "Acme\Gyvfiles\GyvefileBundle\Form\Type" in C:\wamp\www\Symfony\src\Acme\Gyvfiles\GyvefileBundle\Controller\ClientController.php line 22. Do you need to "use" it from another namespace?"
“ClassNotFoundException:尝试从 C:\wamp\www\Symfony\src\Acme\Gyvfiles\GyvefileBundle\Controller\ClientController.php 中的命名空间“Acme\Gyvfiles\GyvefileBundle\Form\Type”加载类“ClientType”。做您需要从另一个命名空间“使用”它吗?”
Can anyone help my with this problem? Thanks!
任何人都可以帮助我解决这个问题吗?谢谢!
采纳答案by qooplmao
I can only assume that you are calling new ClientTypeor the likes in your controller? If so, you should add use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientTypejust under your namespace declaration as otherwise it is trying to find the class ClientTypein the same folder as the calling class.
我只能假设你new ClientType在你的控制器中打电话或喜欢?如果是这样,您应该use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType在命名空间声明下添加,否则它会尝试ClientType在与调用类相同的文件夹中找到该类。
If you are using namespaces then calling a class without a namespace or without it being declared as use'd means it is being called relatively (so in the same directory).
如果您正在使用命名空间,则调用一个没有命名空间或未将其声明为use'd 的类意味着它被相对调用(因此在同一目录中)。
回答by frvade
I just encountered the same issue. It was the wrong name of the file containing the "Type" class. It MUST be the same as the contained class.
我刚刚遇到了同样的问题。这是包含“类型”类的文件的错误名称。它必须与包含的类相同。
Edit:
编辑:
It's about PSR-0 autoloader. It tries to include file based on the namespace of needed class. So given Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientTypenamespace there MUST be a ClientTypeclass in the {pathToSymfony}/Acme/Gyvfiles/GyvefileBundle/Form/Type/folder declared with
这是关于 PSR-0 自动加载器。它尝试根据所需类的命名空间包含文件。所以给定的Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType命名空间必须ClientType在{pathToSymfony}/Acme/Gyvfiles/GyvefileBundle/Form/Type/文件夹中声明一个类
namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;
at the begining of the file.
在文件的开头。
That was the isue in my case. Hope it helps!
这就是我的问题。希望能帮助到你!
回答by IanMcL
I have encountered this as you described, but it went away when I took an underscore out of the class name I'm instantiating. You don't have an underscore in yours, but try changing the name anyway.
正如您所描述的,我遇到过这种情况,但是当我从正在实例化的类名中去掉下划线时,它就消失了。您的名称中没有下划线,但无论如何请尝试更改名称。
回答by Dr.X
For me helped composer dump-autoload --optimize, because previously I executed composer dump-autoload --optimize
对我有帮助composer dump-autoload --optimize,因为以前我执行过composer dump-autoload --optimize
回答by Jessica Hyvert
In your controller ( or in defautController-> indexAction-> of your bundle), if you use a render call with your bundle name like this: "$this->render('MyBundle:Default:index.html.twig');" you need to add a use Bundle like this : "use PathBundle\MyBundle as MyBundle at ";
在您的控制器中(或在您的包的 defautController-> indexAction-> 中),如果您使用带有包名称的渲染调用,如下所示: "$this->render('MyBundle:Default:index.html.twig'); ” 你需要像这样添加一个使用包:“使用 PathBundle\MyBundle 作为 MyBundle 在”;
回答by kalibbala
The time I got that error my entity was like the folowing:
我得到那个错误的时候,我的实体就像下面这样:
/**
* InspectionPlanting
*
* @ORM\Table(name="inspectionplanting")
* @ORM\Entity(repositoryClass="Pencode\ForestryBundle\Entity\ClientProfileRepository")
All I did was to change it to without including my repository:
我所做的只是将其更改为不包括我的存储库:
/**
* InspectionPlanting
*
* @ORM\Table(name="inspectionplanting")
* @ORM\Entity
The last one worked. Hope this helps someone
最后一个有效。希望这有助于某人
回答by Darius.V
When I had similar error:
当我有类似的错误时:
Attempted to load class \"CreateImportJobHandler\" from namespace \"NG\Command\".\nDid you forget a \"use\" statement for another namespace?
试图从命名空间 \"NG\Command\" 加载类 \"CreateImportJobHandler\"。\n您是否忘记了另一个命名空间的 \"use\" 语句?
I was having correct use statement in the class which was using CreateImportJobHandler, but in xml service configuration was bad.
我在使用 CreateImportJobHandler 的类中有正确的 use 语句,但在 xml 服务配置中很糟糕。
Had to be
必须是
<parameter key="ng.command.handler.create_import_job_handler.class">NG\Command\Groups\CreateImportJobHandler</parameter>
回答by Andrey Mischenko
In my case I had a typo in class file name: filename was Syncroniserbut the classname was Synchronizer.
就我而言,我在类文件名中有一个拼写错误:文件名是,Syncroniser但类名是Synchronizer.

