简体   繁体   中英

symfony2 routing with annotations not working

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?

您不小心从routing_dev.yml删除了包含routing.ymlrouting_dev.yml

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM