Symfony2 Autoloader expected class - php

In Symfony2 I get an error:
The autoloader expected class "Website\PublicBundle\Facebook"
to be defined in file
"C:\PHP-XAMPP\htdocs\myProject/src\Website\PublicBundle\Facebook.php".
The file was found but the class was not in it, the class name or
namespace probably has a typo.
500 Internal Server Error - RuntimeException
I cleared cache, restarted Apache and I tried several solutions from similar questions like this, but nothing works.
app/config/config.yml:
services:
facebook:
class: Website\PublicBundle\Facebook
src/Website/PublicBundle/Facebook.php:
namespace Website\PublicBundle\Facebook;
class Facebook {
public function __construct() {
//...
}
}

Your namespace should be
namespace Website\PublicBundle;
Otherwise your class is actually..
Website\PublicBundle\Facebook\Facebook

Related

CakePHP class not found using a custom class

I just started using CakePHP 3. I'm trying to get up and running but doing the simplest of things is proving to be a headache.
I have a class, MySimpleClass, in src/App/MySimpleClass.php
<?php
namespace MyApp\MyNamespace;
class MySimpleClass {
public function aSimpleFunction() {
return 1;
}
}
And in my controller:
<?php
namespace App\Controller;
use Cake\Controller\Controller;
use MyApp\MyNamespace\MySimpleClass;
class MyFirstController extends Controller {
public function display() {
$mySimpleClass = new MySimpleClass();
echo $mySimpleClass->aSimpleFunction();
}
}
But this always gives me:
Error in: ROOT/src/Controller/TestController.php, line 10 Class 'MyApp\MyNamespace\MySimpleClass' not found
I use bin/cake server to run the HTTP server
I added App::className('MyApp\MyNamespace\MySimpleClass'); to bootstrap.php to see if that'd make a difference but it doesn't.
I've run composer dump-autoload on several occasions.
I tried putting MySimpleClass into global namespace but it still gave me the error.
PHPStorm isn't giving me any syntax or naming errors.
You should place your controllers in:
src/Controller
and use:
namespace App\Controller;
Nevermind, I finally found the solution...
I just had to add src to the classmap in composer.json:
"autoload": {
"classmap": [
"src"
],
...,
}
Edit:
Instead of abusing classmap, I just moved my class files to a directory outside of src
If your class is in the file src/App/MySimpleClass.php, the namespace has to be App\App. The first App refers to the root namespace of every CakePHP 3.x application, the second App refers to the subdirectory within the src directory you put your class file into.
If the namespace of the class should be App\MyNamespace, your classfile has to be located in src/MyNamespace.
Also: according to the error message you quoted, your MyFirstController is in a file called TestController.php. Instead, it should be MyFirstController.php. I recommend giving https://book.cakephp.org/3.0/en/intro/conventions.html#file-and-class-name-conventions a read regarding this topic.

namespaceing for service class in symfony2 project

I am facing one problem I do not know what is a reason of this I add class with location in my project src/ApiMaps/ApiMapBundle with this name space
<?php
namespace ApiMaps\ApiMapService;
class ApiMapService {
private $transport;
public function __construct() {
$this->transport = 'sendmail';
}
// ...
}
when i give in
src/config/service.yml
app.test:
class: ApiMaps\ApiMapService\ApiMapService
arguments: ["#doctrine.orm.entity_manager"]
and when i run it from some other class for example
src/ApiMaps/ApiMapBundle/Command/GetApiCommand.php
class GetApiCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$number = $this->getContainer()->get('app.test');
}
}
it give me error
Fatal error: Class 'ApiMaps\ApiMapService\ApiMapService' not found in D:\xampp\htdocs\ProjectMapApiData\a
pp\cache\dev\appDevDebugProjectContainer.php on line 325
[2016-02-01 08:25:20] php.CRITICAL: Fatal Error: Class 'ApiMaps\ApiMapService\ApiMapService' not found {"
type":1,"file":"D:\xampp\htdocs\ProjectMapApiData\app\cache\dev\appDevDebugProjectContainer.php","
line":325,"level":-1,"stack":[]}
[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "ApiMapService" from namespace "ApiMaps\ApiMapService".
Did you forget a "use" statement for another namespace?
Note--
one thing to mention that when i try to make service from the built-in class of symfony2 classes it does not give me such error. I do know where I need to add the namespace of the class which i recently added with my project that it able to know the class...
You mention that your service in file:
src/ApiMaps/ApiMapBundle
but in config you wrote:
ApiMaps\ApiMapService\ApiMapService.
I think you should house you service in file: src/ApiMaps/ApiMapBundle/ApiMapService/ApiMapService.php,
take namespace: ApiMaps\ApiMapBundle\ApiMapService
and write in config: ApiMaps\ApiMapBundle\ApiMapService\ApiMapService.
Truly believe it'll help you...

Symfony2 cannot load Service

I'm having a problem setting up a service in Symfony. I get the following error when I run the app:
The autoloader expected class "HRPortal\SystemBundle\Services\SessionHandler" to be
defined in file "/usr/local/apache2/htdocs/hrportal/src/HRPortal/SystemBundle/Services/SessionHandler.php".
The file was found but the class was not in it, the class name or namespace probably has a typo.
The class is the following:
<?php
namespace HRPortal\SystemBundle\Services\SessionHandler;
class SessionHandler
{
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function init($user)
{
...
}
....
}
And the declaration of my service is as follow:
parameters:
session_handler.class: HRPortal\SystemBundle\Services\SessionHandler
services:
session_handler:
class: "%session_handler.class%"
I have tried many things that did not work, so I thought I'd ask the community and see what you can come out with.
Thank you
Namespace should be
namespace HRPortal\SystemBundle\Services;
That way the fully qualified class name would be HRPortal\SystemBundle\Services\SessionHandler

Symfony2 File Found Class Was Not In It

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.
Your namespace is wrong, rename it:
from: namespace Priceget\CollectorBundle\Crawler\Amazon;
to: namespace Priceget\CollectorBundle\Crawler;
This error also occurs if you do not put <?php in the beginning of the file.
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.
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 (\).

symfony controller class not found

I am new to symfony and maybe I am missing something really simple but I am not able to spot it. Any help will be much appreciated. I have these two files:
C:\xampp\htdocs\Symfony\src\Apps01\ResourceCalBundle\Resources\config\routing.yml
ResourceCalendar_Login:
pattern: /resourcecalendar/login
defaults: { _controller: AppsRollerResourceCalBundle:Login:DisplayLogin }
C:\xampp\htdocs\Symfony\src\Apps01\ResourceCalBundle\Controller\LoginController.php
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class LoginController
{
public function DisplayLoginAction()
{
return new Response('<html><body>Hello There!</body></html>');
}
}
Yet when I point my browser to http://example.com/Symfony/web/app_dev.php/resourcecalendar/login I get the following error:
The autoloader expected class "Apps01\ResourceCalBundle\Controller\LoginController" to be defined in file "C:\xampp\htdocs\Symfony/src/\Apps01\ResourceCalBundle\Controller\LoginController.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
500 Internal Server Error - RuntimeException
Can someone point out what I am missing when I can see that the class LoginController is definitely there inside the filer?
Thanks
Al
I think first of all you should define a namespace
namespace Apps01\ResourceCalBundle\Controller;
as your error told you here:
"namespace probably has a typo."

Categories