路线"不存在"在Symfony,即使它在主路由文件中声明

时间:2016-01-28 09:43:10

标签: php symfony

以下是相关文件的内容:

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.  

似乎我没有在主路由文件中正确声明路由,或者我在错误的地方声明了它。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:4)

  1. horse_route:
  2. 中删除app/config/routing.yml部分
  3. 将注释从@Route("/walrus/red")更改为@Route("/walrus/red", name="walrus_redirect")
  4. 为句柄/** @Route("/horse", name="horse") */ public function horseAction() { }路径
  5. 声明函数/horse

答案 1 :(得分:4)

您的路线名为horse_route,因此您需要使用

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

答案 2 :(得分:-2)

您的控制器路由似乎是错误的定义,并且为了解决您的URL symfony只声明了/ horse路由。

试试这个:

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