2
votes

I just downloaded symfony2 and am beginning to play with routing via annotations. I have my app/config/routing.yml set in the bundle I created to use annotations, and have deleted the Acme bundle and all routing references to it. That said, I've tried creating a couple different route annotations in my controller like @Route("/") and @Route("/hello/{name}") but I'm always greeted with a 404 error (using the dev environment). If I add the route in routing.yml it works just fine even though the routing is configured to use annotations. For whatever reason, my annotations are seemingly being ignored.

Here is my app/config/routing.yml:

DanDefaultBundle:
  resource: "@DanDefaultBundle/Controller/"
  type:     annotation
  prefix:   /

And here is my controller method:

/**
 * @Route("/")
 * @Template()
 */
public function indexAction()
{
    return array('name' => 123);
}

I've included the Sensio\Bundle\FrameworkExtraBundle\Configuration\Route namespace - everything as far as I can tell is correct with what I've seen in the documentation. What am I overlooking that's causing symfony2 to seemingly ignore my routing annotations? Again, if I add the routes to the routing yaml everything works so my bundle is working - but annotations seem to be ignored.

Thanks!

Dan

UPDATE: It looks like I had to add the routes to routing_dev.yml in addition to routing.yml since I was operating in the dev environment. I suppose that's so you have have different routes in-between development and production? I suppose special care will have to be taken to make sure those routes stay in sync?

2
seems to me like you accidentally removed the inclusion of routing.yml from routing_dev.yml.Raffael
That was indeed it. I needed to add back: _main: resource: routing.ymlDan

2 Answers

4
votes

You accidentally removed the inclusion of routing.yml from routing_dev.yml.

1
votes

if you use Route Prefix in your routing.yml

you must declare about your prefix above your class declaration like that :

/**
 * @Route("/")
 */
class PostController extends Controller
{
    /**
     * @Route("/")
     * @Template()
     */
    public function indexAction()
    {
    }

    /**
     * @Route("{id}")
     * @Template()
     */
    public function showAction($id)
    {
    }
} 

Like into Sensio FrameworkExtra Bundle Documentation