Dr.Knowitall
Dr.Knowitall

Reputation: 10468

Autoloader expected class Symfony2

I'm using symfony 2.3 framework and the autoloader claims to have found the file but no class:

RuntimeException: The autoloader expected class "Sensio\Bundle\
FrameworkExtraBundle\Request\ParamConverter\DateTimeParamConverter" 
to be defined in file "/home/na/auth/vendor/sensio/framework-extra-bundle/
Sensio/Bundle/FrameworkExtraBundle/Request/ParamConverter/
DateTimeParamConverter.php". The file was found but the class was not in it, 
the class name or namespace probably has a typo.

The file this is refering to is shown bellow:

<?php

/*
 * This file is part of the Symfony framework.
 *
 * (c) Fabien Potencier <[email protected]>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use DateTime;

/**
 * Convert DateTime instances from request attribute variable.
 *
 * @author Benjamin Eberlei <[email protected]>
 */
class DateTimeParamConverter implements ParamConverterInterface
{
    /**
     * @{inheritdoc}
     * 
     * @throws NotFoundHttpException When invalid date given
     */
    public function apply(Request $request, ConfigurationInterface $configuration)
    {
        $param = $configuration->getName();

        if (!$request->attributes->has($param)) {
            return false;
        }

        $options = $configuration->getOptions();
        $value   = $request->attributes->get($param);

        $date = isset($options['format'])
            ? DateTime::createFromFormat($options['format'], $value)
            : new DateTime($value);

        if (!$date) {
            throw new NotFoundHttpException('Invalid date given.');
        }

        $request->attributes->set($param, $date);

        return true;
    }

    /**
     * @{inheritdoc}
     */
    public function supports(ConfigurationInterface $configuration)
    {
        if (null === $configuration->getClass()) {
            return false;
        }

        return "DateTime" === $configuration->getClass();
    }
}

Anyway, some detail that might help was that I recently installed Doctrine and ran the commands ...

 2028  php app/console doctrine:schema:create
 2029  php app/console doctrine:generate:entities Auth

After those commands, symfony stopped working. I don't know if this is some weird bug or something. If you need more information, I can post. Thanks for any help.

Upvotes: 7

Views: 13257

Answers (11)

ACs
ACs

Reputation: 1445

If this error popus up, you should check your file names for typos as well. In my case however it said "The file was found but the class was not in it, the class name or namespace probably has a typo", in fact the file name was not correct ("From" instead of "Form")

Upvotes: 0

Daniele Dolci
Daniele Dolci

Reputation: 884

It happens to me when I declare wrong namespace.
Example: Namespace AppBundle\Controller;

but the class is at this path: AppBundle\RestController

Upvotes: 0

Czar Pino
Czar Pino

Reputation: 6314

Felt like every answer I read would solve it but didn't. In my case it was ultimately OpCache. Forgot I had it turned on during development. Solution is to either restart the PHP-FPM or Httpd (when using mod_php) everytime the a change is made to the codebase or disable OpCache completely.

Upvotes: 0

Steven Pribilinskiy
Steven Pribilinskiy

Reputation: 2002

In my case it was a typo in Class name. I got an error in AssetsCommand.php when class name was named as AsssetsCommand

Make sure that class file's base name is identical to class name.

Upvotes: 0

MilanG
MilanG

Reputation: 7114

Missing (or short) PHP opening tag can cause that error too. Yes it sounds funny, but if you just follow Symfony example and copy/paste whole class you may not notice that (as I didn't).

Upvotes: 17

Vasu
Vasu

Reputation: 1

I resolved this issue by removing an extra \ before Doctrine as in:

 <entity repository-class="ACC\Bundle\MGLBundle\\\Doctrine\ORM\PersonRepository" 
         name="ACC\Bundle\MGLBundle\Entity\Person" >`

I used the generate:doctrine:entity utility to make the entity as well as the orm.xml file. This utility seems to have a bug and it adds an extra \ in the xml file as above. Removing this extra backslash \ resolved my problem. So, check your orm.xml file after you use the command php app/console doctrine:generate:entity to make sure that there are no extra backslashes in the path. This bug may have been resolved in the later versions of Symfony, but just in case if you get this error, this could be the reason.

Upvotes: 0

develth
develth

Reputation: 782

Answer of @MilanG helped me! I switched my PHP configuration from

short_open_tag = Off

to

short_open_tag = On

Upvotes: 0

Radu Murzea
Radu Murzea

Reputation: 10900

If anyone else has this issue and it doesn't go away by clearing the cache, then I recommend removing the entire folder with your composer dependencies and re-executing composer install.

It worked for me :) .

Upvotes: 2

jwBurnside
jwBurnside

Reputation: 867

Be certain that your namespace is defined in the class that you are trying to use. For instance, in my controller I had:

use Acme\DemoBundle\Amazon\AmazonProductAPI;

This was the correct path to my AmazonProductAPI class, but the system didn't recognize it until I added the proper namespace to the class:

namespace Acme\DemoBundle\Amazon;

Upvotes: 0

AJ Cerqueti
AJ Cerqueti

Reputation: 716

This is a little old now, but in case anyone else gets here by search: This is undoubtedly a cache issue. Manually delete the contents of app/cache/dev and app/cache/prod, and everything should resolve.

Upvotes: 5

Dr.Knowitall
Dr.Knowitall

Reputation: 10468

I think there's a bug in Symfony2. They recently updated to a new version (2.3.1) of symfony, which I think must of broke something. Anyway, I had to comment out //new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle() line in the AppKernel.php file to get rid of the error.

Upvotes: 0

Related Questions