Symfony2 File Found Class Was Not In It
Asked Answered
M

4

5

This is my first question, besides I'm not english-native speaker, so sorry in advance for newbie mistakes...

I'm starting with Symfony2, and I've been facing an autoload problem for a couple of days, i'm getting crazy..

I'm just trying to use a PHP class inside my DefaultController of my AppBundle. I've read the way of doing this is by creating a service in my config.yml and giving a namespace to that class that matches.

Symfony tells me that it does found the file but the class is not in it, the exact error is:

The autoloader expected class "Priceget\CollectorBundle\Crawler\Amazon" to be defined in file "/srv/www/lol.com/public_html/priceget/symfony/src/Priceget/CollectorBundle/Crawler/Amazon.php". The file was found but the class was not in it, the class name or namespace probably has a typo.

And my class is just this:

<?php

namespace Priceget\CollectorBundle\Crawler\Amazon;

use Symfony\Component\HttpFoundation\Response;

class Amazon
{

    public function getAll()
    {
        return new Response('l0l');
    }
}

In my DefaultController I'm calling it like that:

<?php

namespace Priceget\CollectorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Guzzle\Http\Client;
use Symfony\Component\DomCrawler\Crawler;
use Priceget\CollectorBundle\Crawler\Amazon;

class DefaultController extends Controller
{

    public function indexAction()
    {
        $amazon = $this->get('amazon.crawler');
    }
}

And my config.yml piece:

services:
    amazon.crawler:
        class: Priceget\CollectorBundle\Crawler\Amazon

I've already tried to:

  • Empty cache
  • Restart apache
  • Extend the class to Controller? :-Z

Thank you so much in advance.

Matriculate answered 29/9, 2013 at 17:4 Comment(0)
B
12

Your namespace is wrong, rename it:

from: namespace Priceget\CollectorBundle\Crawler\Amazon;

to: namespace Priceget\CollectorBundle\Crawler;

Bandurria answered 29/9, 2013 at 18:30 Comment(4)
By doing this, it returns File not found, autoload error. Btw, my folder structure is: src/Priceget/CollectorBundle/Crawler/Amazon. Any suggestion?Matriculate
If you have "Amazon" folder and "Amazon.php" file in it then you need to change service class definition to: Priceget\CollectorBundle\Crawler\Amazon\AmazonColous
I dont, I missed the .php piece :P The file structure is: src/Priceget/CollectorBundle/Crawler/Amazon.php and if I get rid of 'Amazon' system raises a: FatalErrorException: Error: Class 'Priceget\CollectorBundle\Crawler' not found in /srv/www/domain.es/public_html/priceget/symfony/app/cache/dev/appDevDebugProjectContainer.phpMatriculate
You were 100% right, the problem was that im so stupid that i was getting rid of the Amazon at the config.yml ^^ So that Exception..Matriculate
L
5

This error also occurs if you do not put <?php in the beginning of the file.

Longshoreman answered 21/6, 2017 at 14:24 Comment(0)
F
1

In addition to what's said by Igor, you obviously have to change the FQN class name in the service declaration (YML) if you want it to work.

Fries answered 30/9, 2013 at 9:45 Comment(2)
Amazon.php class is in: src/Priceget/CollectorBundle/Crawler/ by changing the namespace as Igor suggest, Symfony raises a Class not found error. Maybe I'm missing some basics? What's the meaning of FQN? (sorry for newbie questioning)Matriculate
FQN stands for "Fully Qualified Name", this means the class name with the namespace. It's only a matter of changing the classname in both places: in the class declaration itself (php) and in the service declaration (yml) :)Extremism
F
1

This can be a bit misleading, it also happens if you don't extend your class correctly. In my instance I tried to extend a repository with an incorrect FQN:

class FilesRepository extends Doctrine\ORM\EntityRepository

should have been:

class FilesRepository extends \Doctrine\ORM\EntityRepository

Notice the missing backslash (\).

Fourposter answered 8/8, 2017 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.