以下是相关文件的内容:
app/config/routing.yml
的内容:
horse_route:
path: /horse
defaults: { _controller: AppBundle:Horse:show }
app:
resource: "@AppBundle/Controller/"
type: annotation
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);
}
}
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.');
}
}
当我在浏览器中输入localhost:8000/walrus/red
时,收到错误消息
Unable to generate a URL for the named route "/horse" as such route does not exist.
似乎我没有在主路由文件中正确声明路由,或者我在错误的地方声明了它。任何帮助表示赞赏。
答案 0 :(得分:4)
horse_route:
app/config/routing.yml
部分
@Route("/walrus/red")
更改为@Route("/walrus/red", name="walrus_redirect")
/** @Route("/horse", name="horse") */ public function horseAction() { }
路径/horse
醇>
答案 1 :(得分:4)
您的路线名为horse_route
,因此您需要使用
return $this->redirectToRoute('horse_route', array(), 301);
答案 2 :(得分:-2)
您的控制器路由似乎是错误的定义,并且为了解决您的URL symfony只声明了/ horse路由。
试试这个:
app:
resource: "@AppBundle/Controller/"
type: annotation
prefix: /