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
Related
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',
]);
}
}
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.
I have added a second bundle and when I try to open some url from that bundle, I keep getting an error that the route was not found. Adding the same route to the main bundle works perfectly.
What's wrong?
This is my project structure. I also:
- added the UserBundle to AppKernel.php (IDE shows the class exists)
- use AppBundle\Controller namespace in AppBundle, and UserBundle\Controller namespace in UserBundle
The controller I try to access from the UserBundle looks like this:
namespace UserBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
/**
* #Route("/login", name="user_login")
*/
public function loginAction(Request $request)
{
return array();
}
}
I suspect you need to add the UserBundle to your routing.yml configuration. You should have something like this in the routing.yml:
user_bundle:
resource: "#UserBundle/Controller/"
type: annotation
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:
I have create new bundle in src\Moda\CategoryBundle\Controller\DefaultController.php
and a change routing to:
namespace Moda\CategoryBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
/**
* #Route("/show", name="_show")
* #Template()
*/
public function indexAction()
{
die('test');
return array();
}
}
and my routing.yml in app/config
moda_category:
resource: "#ModaCategoryBundle/Controller/"
type: annotation
prefix: /
This links dosnt work:
localhost/web/app_dev.php/category/show
localhost/web/app_dev.php/show
Do you know what I am doing wrong?
I think you should import the config.yml file inside your bundle.
So instead of :
moda_category:
resource: "#ModaCategoryBundle/Controller/"
type: annotation
prefix: /
Change it to:
moda_category:
resource: "#ModaCategoryBundle/Resources/config/routing.yml"
type: annotation
prefix: /
And then add the routes you need inside that file.