Why is my Symfony route is not working? - php

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

Related

No route found from another bundle

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

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

Symfony2 Route annotation on class not working

So basically I have a controller of a bundle in which i want to and a route prefix so i use the #Route annotation on the class, i've done this all of the other controllers of my Symfony2 app. However this one does not take the prefix into account so instead of being able to access the page on /admin/users/list i only access it on /list.
Here is the controller :
<?php
namespace LanPartyOrg\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use JMS\SecurityExtraBundle\Annotation\PreAuthorize;
/*
* #Route("/admin")
*
*/
class AdminController extends Controller
{
/**
* #Route("/list", name="users_list")
* #Template("LanPartyOrgUserBundle:Admin:List.html.twig")
*/
public function listAction(){
$em = $this->getDoctrine()->getManager();
$users = $em->getRepository('LanPartyOrgUserBundle:User')->findAll();
return array('users'=>$users);
}
}
And here is my routing.yml :
lan_party_org_user:
resource: "#LanPartyOrgUserBundle/Controller/"
type: annotation
prefix: /
fos_user_security:
resource: "#FOSUserBundle/Resources/config/routing/security.xml"
fos_user_profile:
resource: "#FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /profile
fos_user_register:
resource: "#FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /register
fos_user_resetting:
resource: "#FOSUserBundle/Resources/config/routing/resetting.xml"
prefix: /resetting
fos_user_change_password:
resource: "#FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /profile
Thanks for any help
Annotations must be added to docblocks, not just simple comments.
You need to start your comment with /** instead of /* (notice double *):
/**
* #Route("/admin")
*/
class AdminController extends Controller
{
// ...
}
This is going to prefix all your AdminController's routes with /admin.
Your following code
/*
* #Route("/admin")
*
*/
should be as
/**
* #Route("/admin")
*
*/
and also make sure that two controller should not have same prefix .

symfony 2 cannot find the route i've created

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

Categories