以下是相关文件的内容:
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.
似乎要么我没有在主路由文件中正确声明路由,要么我在错误的位置声明了它。任何帮助表示赞赏。
您的路由称为horse_route
,因此您需要使用
return $this->redirectToRoute('horse_route', array(), 301);
- 从
app/config/routing.yml
上拆下horse_route:
部件 - 将批注从
@Route("/walrus/red")
更改为@Route("/walrus/red", name="walrus_redirect")
- 为句柄
/horse
路由声明函数/** @Route("/horse", name="horse") */ public function horseAction() { }
似乎您的控制器路由定义错误,并且为了解析您的 URL symfony 只声明了/horse 路由。
试试这个:
app:
resource: "@AppBundle/Controller/"
type: annotation
prefix: /