vazgen vazgen - 5 months ago 21
PHP Question

ClassNotFoundException: Attempted to load class... Symfony

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:

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:

<?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:

$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:

Update: 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.

"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?"

Can anyone help my with this problem? Thanks!

Answer

I can only assume that you are calling new ClientType or the likes in your controller? If so, you should add use Acme\Gyvfiles\GyvefileBundle\Form\Type\ClientType just under your namespace declaration as otherwise it is trying to find the class ClientType in the same folder as the calling class.

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).