Controller class not found for Symfony 3 route - php

I am getting the error
`The _controller value "AppBundle:GasSupplier:overview" maps to a
"AppBundle\Controller\GasSupplierController" class, but this class was not found. Create
this class or check the spelling of the class and its namespace.`
But as far as I can tell, the class does exist, with the correct namespace, in the correct directory. My app/config/routing.yaml is
AppBundle:
resource: "#AppBundle/Resources/config/routing.yml"
and the contents of my src/AppBundle/Resources/config/routing.yaml is
homepage:
path: /
defaults: { _controller: AppBundle:Default:index }
gassupplieroverview:
path: /gassuppliers
defaults: { _controller: AppBundle:GasSupplier:overview }
and the contents of my src/AppBundle/Controller/GasSupplier.php is
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class GasSupplierController extends Controller
{
public function overviewAction(Request $request)
{
$gas_suppliers = $this->getDoctrine()->getEntityManager()->getRepository('AppBundle:GasSupplier')->findAll();
return $this->render('gas_supplier/overview.html.twig', ['gas_suppliers' => $gas_suppliers]);
}
}
All the other answers I have found about this, the answer was almost always a typo - I have checked and checked again and I don't think I have any typos...
Using Symfony 3.

Typo: GasSupplierController in the class name, vs GasSupplier.php as the filename.
Wherever it says GasSupplier (including the filename), should be renamed/changed-to GasSupplierController, to match the class name.

Related

Routes are declared in routes.yaml but symfony don't found them. Even when i try using annotations

Ihave a symfony app that ignore the roytes i can declare.
For exemple, i have this lines of code in routes.yaml :
app_titi
path: /titi
methods: ['GET']
defaults:
_controller: 'App\Controller\TitiController::index'
I do a cache:clear and when i try to watch the result in my browser, no route found.
The controller exists and have the right name.
My context is Symfony 6.2, PHP 8.1, runing in Docker containers.
I tryed to create a new controller, i declared it in routes.yaml, same results.
I tryed to create a controller but this time using annotations, same results.
When i ask the command router:debug, symfony console returns an empty results.
Thanks for helping!
For a better answer please provide your controller path and code example.
In Symfony 6.2, by default, you do not have to change routes.yaml since all route declared in controller are auto detected if you keep this inside routes.yaml.
Here is my routes.yaml for example :
controllers:
resource: ../src/Controller/
type: annotation
kernel:
resource: ../src/Kernel.php
type: annotation
Then inside the controller
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
// This not annotation but php attribute, i advise you to use it instead
#[Route('/', name: "home")]
public function home(): Response
{
return $this->render('website/index.html.twig');
}
}
Read this to understand why symfony now use php attribute :
https://symfony.com/blog/new-in-symfony-5-2-php-8-attributes
#ThomasL
Hi, here is my routes.yaml :
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute
app_titi:
path: /titi
methods: ['GET']
defaults:
_controller: 'App\Controller\TitiController::index'
Others routes are now declared in attributes.
This is my Titi controller :
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class TitiController extends AbstractController
{
#[Route('/titi', name: 'app_titi')]
public function index(): Response
{
return $this->render('titi/index.html.twig', [
'controller_name' => 'TitiController',
]);
}
}

Symfony where to initialize routes? (routing.yml or inside controller)

On the routing page of smyfony is an routing example (the first one). Now they give us 4 options of code (Annotations, YAML, XML, PHP). Where is the difference? And maybe you can take a look on my Controller + Route.
The controller looks like this:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class ToDoListController extends Controller
{
/**
* #Route("/ToDoList", name="ToDoList")
*/
public function showToDoList()
{
}
}
?>
Now I added this route into the routing.yml too.
ToDoList:
path: /Aufgabenliste
defaults: {_controller: AppBundle\Controller \ToDoListController::showToDoList}
Is that correct? Whats about the path? In the first example of the symfony page they wrote defaults: { _controller: AppBundle:Blog:show } but in the description they wrote:
The _controller string is called the logical name. It follows a pattern that points to a specific PHP class and method, in this case the AppBundle\Controller\BlogController::listAction and AppBundle\Controller\BlogController::showAction methods.
Allright. So in Symfony you get to choose between different methods of routing. You can do this for example via annotations or yml(I used to use yml but now I switched to annotations). The only difference is in... format of the file ;) For example I realy like to use annotations because instead of having one billion entries in one file I have every route right next to the code it leads to. At this point for you it depends on what you like better. However I think it's considered as good practice to use annotations.
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class ToDoListController extends Controller
{
/**
* #Route("/ToDoList", name="ToDoList")
*/
public function showToDoList()
{
}
}
?>
Code above is ok - you used required namespaces and created correct annotation. All of this you have to do in your bundle you are working on. Also you have to go to app/config/routing.yml and put there something like this:
YourBundleName:
resource: "#YouBundle/Controller/"
type: annotation
Of course it's just an example - you need to adjust it to your needs. This way you are saying to Symfony that you want to use annotations.

Symfony2 won't pass arguments to a service

I am trying to pass arguments to my new Controller in new Bundle I have created via cli ( I have tried to do it manually too ). It can be anything, string, service, parameter from parameters.yml file, nothing comes through.
Error:
{"code":500,"message":"Warning: Missing argument 1 for MyProject\\PosBundle\\Controller\\OfferController::__construct(), called in \/var\/www\/vhosts\/httpdocs\/myproject\/vendor\/symfony\/symfony\/src\/Symfony\/Component\/HttpKernel\/Controller\/ControllerResolver.php on line 162 and defined","errors":null}
My Files are
service.yml
services:
myproject_pos_offer_controller:
class: MyProject\PosBundle\Controller\OfferController
arguments: ['templating']
I have tried to do this:
services:
myproject_pos_offer_controller:
class: MyProject\PosBundle\Controller\OfferController
arguments:
someString: 'templating'
OfferController:
class OfferController extends RestController
{
private $someString;
public function __construct($someString)
{
$this->$someString = $someString;
}
public function indexAction(){
}
}
What am I doing wrong or what did I forget to do?
And Cerad was right (Thanks for help!). I had to pass my controller in my routing.yml configuration as a service. Do find that I had to debug ControllerResolver.php, that was fun.
Solution
Code above is correct. The problem was lying in my routing.yml
Wrong
myproject_pos.offer_create:
path: /{store_hash}/offers
defaults: { _controller: MyProjectPosBundle:Offer:create }
methods: 'POST'
Correct
myproject_pos.offer_create:
path: /{store_hash}/offers
defaults: { _controller: myproject_pos_offer_controller:createAction }
methods: 'POST'
As you see the key is in defaults attrubute.

More than one controller in one Bundle (Symfony)

Is it possible to do more than one controller in Symfony? And if yes, how can I do it? (Controller, routing etc)
Because now I have:
Directory "Controller" and inside "DefaultController.php" (It has 1000 lines of code like now.)
Directory Resources->Config->Routing.yml (Here I have all routings).
Directory Resources->views->Default (In this dir I have all views).
Is it possible to make more controllers? For example one controller will have only indexAction(), other will have addclientAction() etc.
So you will have controlelrs at src/MyApp/SomeBundle/Controller/
class OneController extends Controller
{
public function indexAction()
{
....
}
}
class TwoController extends Controller
{
public function addclientAction()
{
....
}
}
you routing.yml should looks like
my_route_index:
pattern: /
defaults: { _controller: MyAppSomeBundle:One:index }
my_route_addclient:
pattern: /addclient/
defaults: { _controller: MyAppSomeBundle:Two:addclient }
Yes, of course it is possible.
You can see an example here:

Symfony 2 - No route found for "GET /videotheque"

I need some help because I didn't succeed in finding the source of my route problem.
Here is my routing.yml:
gstyle39VideothequeBundle:
resource: "#gstyle39VideothequeBundle/Resources/config/routing.yml"
prefix: /videotheque
The routing.yml in VideothequeBundle/Resources/config/:
VideothequeBundle_homepage:
pattern: /
defaults: { _controller: gstyle39VideothequeBundle:Videotheque:index }
My controller:
<?php
namespace gstyle39\VideothequeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
class VideothequeController extends Controller
{
public function indexAction()
{
return new Response("Kikoo");
}
}
For my part I did a "cache:clear", a "router:debug" which detected my route:
VideothequeBundle_homepage ANY /videotheque/
I also manually deleted the folder app/cache...
IMHO you didn't add routing definition in application global routing.yml (app/config/routing.yml).
Look at my routing definitions in https://github.com/drupalmk/Jobeeto

Categories