Search code examples
controllerroutessymfony4

symfony 4.1 Unable to generate a URL


in my synfony app I have two controllers: The ActionController is supposed to simply render a template containing a form. Submitting the form, I want to send a GET request to the SpreadSheetControllers getSpreadSheet() method.

This is the ActionController:

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class ActionController extends AbstractController {

  /**
   * @Route("/action")
   */
  public function action() {

    $form = $this->createFormBuilder()
        ->setAction($this->generateUrl('/spreadSheet'))
        ->setMethod('GET')
        ->add('save', SubmitType::class, array('label' => 'Action'))
        ->getForm();

    return $this->render('action.html.twig', array(
        'form' => $form->createView(),
    ));

  }

}

And here is the SpreadSheetController:

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

class SpreadSheetController extends AbstractController {

  /**
   * @Route("/spreadSheet")
   */
  public function getSpreadSheet() {
    return new Response(
        '<html><body>spreadSheet</body></html>'
    );
  }

}

Still, when browsing http://localhost:8000/action I get a RouteNotFoundException: Unable to generate a URL for the named route "/spreadSheet" as such route does not exist.

Does anybody know why the route isn't found??


Solution

  • You have to reference the name of the route, not the actual url. Name your route like so:

    /**
     * @Route("/spreadSheet", name="spreadsheet")
     */
    

    Then reference that name in your ActionController:

        ->setAction($this->generateUrl('spreadsheet'))