Symfony2 routing with annotations

20,891

Solution 1

# app/config/routing.yml

MunichInnovationGroupPatentBundle:

    resource: "@MunichInnovationGroupPatentBundle/Controller/DefaultController.php"
    type:     annotation
    prefix:   /

The controller should have:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

Solution 2

If you are in the developement environment check you import the routing.yml in routing_dev.yml:

# app/config/routing_dev.yml

_main:
    resource: routing.yml
Share:
20,891
Zoha Ali Khan
Author by

Zoha Ali Khan

Updated on December 03, 2020

Comments

  • Zoha Ali Khan
    Zoha Ali Khan over 3 years

    I am trying to define my routes using annotations in symfony2. My Bundle name is PatentBundle. But I am getting an error of

    No route found for "GET /portfolio/
    

    My app/config/routing.yml

    MunichInnovationGroupPatentBundle:
    resource: "@MunichInnovationGroupPatentBundle/Controller/"
    type:     annotation
    prefix:   /
    defaults:  { _controller: "MunichInnovationGroupPatentBundle:Default:index" }
    

    My portfolio controller looks like

    <?php
     namespace MunichInnovationGroup\PatentBundle\Controller;
    
     use MunichInnovationGroup\PatentBundle\Entity\Log;
    
     use MunichInnovationGroup\PatentBundle\Entity\UserPatent;
     use Symfony\Component\HttpFoundation\Response;
     use Symfony\Component\HttpFoundation\Request;
     use JMS\SecurityExtraBundle\Annotation\Secure;
     use Symfony\Component\Security\Core\Exception\AccessDeniedException;
     use Symfony\Bundle\FrameworkBundle\Controller\Controller;
     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
     use MunichInnovationGroup\PatentBundle\Entity\SvPatents;
     use MunichInnovationGroup\PatentBundle\Entity\PmPortfolios;
     use MunichInnovationGroup\PatentBundle\Entity\UmUsers;
     use MunichInnovationGroup\PatentBundle\Form\PatentType;
     use MunichInnovationGroup\PatentBundle\Entity\PmPatentgroups;
     use Symfony\Component\Security\Core\SecurityContext;
     use MunichInnovationGroup\PatentBundle\Util\SecurityHelper;
     use Exception;
    
    /**
     * Portfolio controller.
     * @Route("/portfolio")
    */
    class PortfolioController extends Controller {
    
    /**
     * Index action.
     *
     * @Route("/", name="portfolio")
     * @Method({"GET", "POST"})
     * @Template("MunichInnovationGroupBundle:Portfolio:show.html.twig")
     */
    public function indexAction(Request $request) {
          // method code goes here
        }
    
  • Zoha Ali Khan
    Zoha Ali Khan almost 12 years
    I want to import the whole controller directory not only the default controller. I am getting error on the portfolio controller
  • Visavì
    Visavì almost 12 years
    Your code whould work... As suggested by Simone Demo Gentili, you should see all your active routes using app/console router:debug what is the result?
  • Zoha Ali Khan
    Zoha Ali Khan almost 12 years
    I checked it through aap/console and I have two bundles 1 is Bundle and the other is PatentBundle. Bundle also has portfolio route and its path is v1/portfolio, I want to make the PatentBundle routes work not the others.
  • Visavì
    Visavì almost 12 years
    So you have to understand why your route is v1/portfolio and not /portfolio. This didn't appear in the code you post.
  • Zoha Ali Khan
    Zoha Ali Khan almost 12 years
    yeah i just run the command u provided and it showed me that the path for portfolio rout is v1/portfolio not portfolio. but how can I use Patentbundle routes.
  • mattalxndr
    mattalxndr about 11 years
    Thanks for the "The controller should have:" section. You need to include all of those, even if they are not being used.
  • hardik
    hardik almost 10 years
    is there any way to specify @Method at class level ?