Daniel Cole
Daniel Cole

Reputation: 51

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

Upvotes: 1

Views: 1518

Answers (2)

Ajeet Varma
Ajeet Varma

Reputation: 726

Your following code

          /*
           * @Route("/admin")
           * 
           */

should be as

         /**
           * @Route("/admin")
           * 
           */

and also make sure that two controller should not have same prefix .

Upvotes: 2

Jakub Zalas
Jakub Zalas

Reputation: 36191

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.

Upvotes: 3

Related Questions