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."
Related
I'm trying to refactor some controllers in Symfony 5 server, but suddenly I'm unable to change or create controllers because of the following error:
'App\Controller{{ControllerName}}' has no container set, did you forget to define it as a service subscriber?
This is the controller:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Shiftsummary;
use App\Entity\Station;
use App\Entity\Shift;
use App\Entity\Line;
use \Datetime;
class StartStationController extends AbstractController {
/**
* #Route("/start", name="StartStation")
*/
public function route(Request $request)
{
...
} }
This is the content of service.yaml
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
The controllers are inside the src/controller/ folder
The issue had to do with the cached files, deleting /var/cache made the error not occur.
Thanks go to Cerad for point this out.
I have to add that if you set the wrong namespace, you get this error too:
// src/Controller/Api/ProductController.php
namespace App\Controller;
class ProductController extends AbstractController {}
namespace must follow folders structure.
namespace App\Controller\Api;
this is just it, your controller did not have container injected. However, framework expects it to be done if your controller inherits from AbstractController.
in ControllerResolver.php:
TLDR; What you need is to inject it.
In your services.yaml, add the following setter call
In my case, the class had just a different name than the file.
You have to change:
namespace App\Controller;
with:
namespace App\Controller\Api;
None of above helped in my case.
I solved my case by:
calls:
- [setContainer, ["#service_container"]]
https://symfony.com/doc/5.4/service_container/calls.html
In my case it was an error in the controller name when I used the bin/console command :
bin/console make:controller APIController this command add automatically suffix -Controller to the file.
Then the controller created is called APIControllerController.
Next I made a copy/paste of Auth0 guide to create API nammed APIController.
In the end the file had the name APIControllerController.php and the class APIController: you just had to change the filename and remove the duplicate -Controller suffix.
Or - in general - verify the name of file controller and the class name ;)
(Another possibility, even more basic than the other suggestions:)
I had a simple controller that was requiring another PHP file, but in the course of some changes that required file couldn't even be compiled. Fixing that required file resolved the somewhat misleading "has no container set..." message.
I have just resolved this issue.
Turned out for me - I had miss typed __construct() as a private function, should be public
Attempted to load interface "NotExistingInterface" from namespace "Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation".
Did you forget a "use" statement for another namespace? unable to understand this error
Symfony\Component\Debug\Exception\
ClassNotFoundException
in \vendor/symfony/framework-bundle/Tests/Fixtures/Validation/Article.php (line 5)
<?php
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation;
class Article implements NotExistingInterface
{
public $category;
}
I don't know the error. Please help.
Your code should be like:
<?php
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation;
use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\NotExistingInterface;
class Article implements NotExistingInterface
{
public $category;
}
It's likely that you have a class in your code called Article, and while using it in some code, forgot a use statement for it.
I ran into the same issue, which is why I'm here =).
It turned out that I hadn't put a use statement for my Article class in some other code, and when executing, it must have decided to use the Article class from the FrameworkBundle tests. Probably some autoloading/autowiring weirdness.
I just had the same problem.
In a controller I needed my Article entity and the IDE suggested me to import the class and I clicked on the wrong one.
Check your imports (use)
You might have a...
use Symfony\FrameworkBundle\Tests\Fixtures\Validation\Article;
instead of a
use App\Entity\Article;
I have started building a new Web App using Symfony, but am having issues using classes.
PhpStorm is able to find the functions within the classes (due to the fact that it gives suggestions when you type $className->.
Also, to prove this is not the same as the other similar questions, I have over simplified it, and even so, the error still occurs.
I have the below in my DefaultController:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use testBundle;
use AppBundle\domainionClasses\firstlevelchecks;
class DefaultController extends Controller
{
/**
* #Route("/register",name="register")
*/
public function registerAction(){
$testClass = new firstlevelchecks();
$testing = $testClass->donothing(); //the IDE knows that the function donothing() exists, in fact it suggests it.
return new Response('');
}
}
The below is the php class located in AppBundle/domainionClasses/test1.php
<?php
namespace AppBundle\domainionClasses;
class firstlevelchecks
{
function donothing(){
return null;
}
}
When loading the /register route, the below Symfony error is displayed:
Attempted to load class "firstlevelchecks" from namespace "AppBundle\domainionClasses".
Did you forget a "use" statement for another namespace?
It is attempting to load the class from the correct name space, and I have used a use statement.
Is there anything I am missing here, or is there a problem with Symfony? This is the first time I used the new version of PhpStorm, and have just downloaded the plugin, also the first time I have experienced this issue :(
Because your file is named test1.php, how can the autoloader know which file to include ?
You shoud rename it to firstlevelchecks.php (= the name and case of your class).
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
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 (\).