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);