Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route "does not exist" in Symfony even though it's declared in main routing file

Tags:

php

symfony

Here are the contents of the relevant files :

Contents of app/config/routing.yml :

horse_route:
    path:   /horse
    defaults: { _controller: AppBundle:Horse:show }


app:
    resource: "@AppBundle/Controller/"
    type:     annotation

Contents of src/AppBundle/Controller/WalrusController.php :

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class WalrusController extends Controller
{

    /**
     * @Route("/walrus/red")
     */
    public function walrusRedirect()
    {

      return $this->redirectToRoute('/horse', array(), 301);

    }   
}

Contents of src/AppBundle/Controller/HorseController.php :

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class HorseController extends Controller
{

    public function showAction()
    {
      return new Response('This is a horse.');
    }

}

When I type localhost:8000/walrus/red in my browser, I get the error message

Unable to generate a URL for the named route "/horse" as such route does not exist.  

It seems that either I did not declare the route correctly in the main routing file, or that I declared it in the wrong place. Any help appreciated.

like image 952
Ewan Delanoy Avatar asked Jan 28 '16 09:01

Ewan Delanoy


2 Answers

Your route is called horse_route so you would need to use

return $this->redirectToRoute('horse_route', array(), 301);
like image 114
qooplmao Avatar answered Oct 27 '22 08:10

qooplmao


  1. Remove horse_route: part from your app/config/routing.yml
  2. Change your annotation from @Route("/walrus/red") to @Route("/walrus/red", name="walrus_redirect")
  3. Declare a function /** @Route("/horse", name="horse") */ public function horseAction() { } for handle /horse route
like image 39
Mert Öksüz Avatar answered Oct 27 '22 08:10

Mert Öksüz