Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException: Attempted to load class... Symfony

Tags:

php

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!

like image 626
vazgen Avatar asked Dec 08 '13 01:12

vazgen


2 Answers

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\ClientType namespace there MUST be a ClientType class in the {pathToSymfony}/Acme/Gyvfiles/GyvefileBundle/Form/Type/ folder declared with

namespace Acme\Gyvfiles\GyvefileBundle\Form\Type;

at the begining of the file.

That was the isue in my case. Hope it helps!

like image 183
frvade Avatar answered Sep 19 '22 20:09

frvade


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.

like image 37
IanMcL Avatar answered Sep 22 '22 20:09

IanMcL