No route found from another bundle - php

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

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 No route found

I want to make a new page, so I created a new bundle to create a new route to another page but there is a issue with routes 'No route found for "GET/products" '
Here is my controller
<?php
namespace MyApp\ProductBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* #Route("/products")
*/
public function indexAction()
{
return $this->render('ProductBundle:Default:index.html.twig');
}
}
this is my route
product:
resource: '#ProductBundle/Controller/'
type: annotation
The route appear when I execute the command php bin/console debug:router like this :
product ANY ANY ANY /products
I execute the command server: start and then server:run that the probleme still not fixed
can any one help me please

Symfony Route not found

I'm really new with symfony, and I have strange problem.
I have a default controller, which looks like:
Both Controllers located:
src/AppBundle/Controller/
Names:
DefaultController.php
and
CmsController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* #Route("/")
*/
public function indexAction(Request $request)
{
die('Homepage');
}
}
And I'm trying to create new one:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class CmsController extends Controller
{
/**
* #Route("/cms")
*/
public function cmsAction(Request $request)
{
die('Cms Page');
}
}
Routing file looks like:
app:
resource: "#AppBundle/Controller/"
type: annotation
When I try to go for www.domainname.com - default controller shows "Homepage" - as it should.
When I try to go for www.domainname.com/cms - it gives error 404.
What can be the problem?
Probelm was simply with cache. Clearing it solved the problem.

My first controller in symfony

// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function numberAction()
{
$number = rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
I'm new of Symfony framework. I tried this simple code without any result.
That's the server response:
No route found for "GET /lucky/number" 404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
I don't now why the Default controller use the annotation and I can see the homepage of my Symfony application.
I think you need to import your annotation routes. Symfony will scan locations you mention in app/config/routing.yml.
# app/config/routing.yml
lucky:
resource: "#AppBundle/Controller/LuckyController.php"
type: annotation
See Symfony docs for more details
Also try clearing the cache, just to be sure.
You're missing an important part of the route and its name:
/**
* #Route("/lucky/number", name="lucky_number")
*/
More information: http://symfony.com/doc/current/book/routing.html

Symfony2: No route found /demo/contactus

I am new to symfony and following the AcmeDemo i created a new page for Contact Us. At the moment it is working for the url localhost:8000/contactus but following the same implementation of demo/secured/login it should be localhost:8000/demo/contactus which gives 404.
I am not sure what wrong i am doing.
routing.yml
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
ContactusController
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class ContactusController extends Controller
{
/**
* #Route("/contactus", name="_demo_contactus")
* #Template()
**/
public function indexAction()
{
return array();
}
}
?>
And then i have my view which is i am sure correct. Please provide the detailed answer so that it can help me in clearing my concepts as well. Thanks !!!
If you take a look at the SecuredController from the AcmeDemoBundle, you'll see that the class declaration has this annotation at the start:
/**
* #Route("/demo/secured")
*/
class SecuredController extends Controller
{
// ...
This annotation prefixes all subsequent routes in the Controller class with /demo/secured. Therefore, you should write your own Controller using the same fashion:
/**
* #Route("/demo")
*/
class ContactusController extends Controller
{
// ...
See the docs concerning Route Prefixing for more information
alternatively, you could also leave the ContactusController class alone and modify your route import instead:
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
prefix: /demo
You're also using the same route name twice, which means Symfony will reload routes and overwrite anything you had before for _demo_contactus. Try the following:
routing.yml
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
ContactusController
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* #Route("/demo/contactus")
*/
class ContactusController extends Controller
{
/**
* #Route("/", name="_demo_contactus_home")
* #Template()
**/
public function indexAction()
{
return array();
}
}
Make sure you try both /demo/contactus and /demo/contactus/.. it may be looking at that slash

Categories