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.
Your route is called horse_route
so you would need to use
return $this->redirectToRoute('horse_route', array(), 301);
horse_route:
part from your app/config/routing.yml
@Route("/walrus/red")
to @Route("/walrus/red", name="walrus_redirect")
/** @Route("/horse", name="horse") */ public function horseAction() { }
for handle /horse
routeIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With