I have created a route named "test" but symfony cannot find it.
Routing_dev.php
_test:
resource: "#AcmetestBundle/Controller/testController.php"
type: annotation
pattern: /test
Added this line to AppKernel.php
$bundles[] = new Acme\testBundle\AcmetestBundle();
Created a dir described below
Acme |
1.1 testBundle
|
1.11 AcmetestBundle.php
|
1.12 Controller
|
1.13 testController.php
AcmetestBundle.php
namespace Acme\testBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmetestBundle extends Bundle
{
public function __construct(){
var_dump("initializing ".__DIR__);
}
}
testController.php
namespace Acme\testBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Acme\DemoBundle\Form\ContactType;
// these import the "#Route" and "#Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class testController extends Controller
{
/**
* #Route("/", name="_demo")
* #Template()
*/
public function indexAction()
{ var_dump(11);
return array("");
}
}
Browser logs :
No route found for "GET /test" 404 Not Found - NotFoundHttpException 1
linked Exception: ResourceNotFoundException »
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route
found for "GET /test" (uncaught exception) at
C:\xampp\htdocs\Symfony\app\cache\dev\classes.php line 4560
I think you meant to write prefix instead of pattern:
_test:
resource: "#AcmetestBundle/Controller/testController.php"
type: annotation
prefix: /test
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 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
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
// 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
I made a new Symfony2 bundle and deleted the Acme bundle.
Then I created a new Controller (MainController.php):
<?php
namespace My\BlogBundle\Controller;
class MainController extends Controller
{
/**
* #Route("/", name="index")
* #Template()
*/
public function indexAction()
{
return array();
}
And a simple view: (Main/index.html.twig) which only contains a hello. My routing.yml is empty. When I run the whole project I get:
No route found for "GET /"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »
What is wrong here and how to solve it?
Here is my routing debug:
\Symfony>php app/console router:debug
[router] Current routes
Name Method Pattern
_wdt ANY /_wdt/{token}
_profiler_search ANY /_profiler/search
_profiler_purge ANY /_profiler/purge
_profiler_info ANY /_profiler/info/{about}
_profiler_import ANY /_profiler/import
_profiler_export ANY /_profiler/export/{token}.txt
_profiler_phpinfo ANY /_profiler/phpinfo
_profiler_search_results ANY /_profiler/{token}/search/results
_profiler ANY /_profiler/{token}
_profiler_redirect ANY /_profiler/
_configurator_home ANY /_configurator/
_configurator_step ANY /_configurator/step/{index}
_configurator_final ANY /_configurator/final
I also cleared the cache with no success.
Here is the routes.yml:
my_blog:
resource: "#MyBlogBundle/Resources/config/routing.yml"
prefix: /
and the routing.yml in MyBlogBundle/Resources/config/routing.yml is empty.
The way your routes.yml is setup, you are requesting the routing.yml file from your bundle.
If you want to use annotations to manage the routes in your bundle, you have to write the routes.yml the following way:
my_blog:
resource: "#MyBlogBundle/Controller/MainController.php"
prefix: /
type: annotation
And your controller needs to include the Route class from the FrameworkExtraBundle:
<?php
namespace My\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class MainController extends Controller
{
/**
* #Route("/", name="index")
* #Template()
*/
public function indexAction()
{
return array();
}
}
This assumes you have installed the SensioFrameworkExtraBundle (http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#installation).
More information on the route annotation: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
For others troubleshooting the situation where the router annotations is correctly installed, but the routes aren't working, you may need to install
composer require symfony/apache-pack
You also could declare your routing for your annotation with:
blog_index:
path: /
defaults: {_controller:BlogBundle:index}
in your BlogBundle/config/routing.yml
and in root, set
blog:
resource: "#BlogBundle/Resources/config/routing.yml"
prefix: /
then in your MainController, the annotation should work:
..use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/", name="blogindex")
*/
Add following annotation for the MainController class:
/**
* #Route("/")
*/
class MainController extends Controller
{
}
you'r problem is that you have referenced the routing as yml not annotations, if you want to use annotations you must declare at your front route att the app folder as
post:
resource: "#AcmeBlogBundle/Controller/PostController.php"
type: annotation
and at the PostController class you define
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/")
*/
class MainController extends Controller
{
}
and the function
/**
* #Route("/add", name="article_add")
*/
public function add(){
...
}
See the referance at the docs