在重定向symfony2之前检查url

在重定向symfony2之前检查url

本文介绍了在重定向symfony2之前检查url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if ($u = $this->generateUrl('_'.$specific.'_thanks'))
  return $this->redirect($u);
else
  return $this->redirect($this->generateUrl('_thanks'));

我不想重定向到 _specific_thanks 网址(如果存在)。那么如何检查网址是否存在?

I wan't to redirect to the _specific_thanks url when it exist. So How to check if a url exist?

这样做的时候,我遇到了这个错误:

When I did that, I had this error:


推荐答案

我不这样做认为没有直接检查路线是否存在的方法。但是您可以通过路由器服务查找路由存在。

I don't think there's a direct way to check if a route exists. But you can look for route existence through the router service.

$router = $this->container->get('router');

然后您可以获取路线集合并调用 get()给定路由,如果不存在则返回null。

You can then get a route collection and call get() for a given route, which returns null if it doesn't exist.

$router->getRouteCollection()->get('_'. $specific. '_thanks');

这篇关于在重定向symfony2之前检查url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:29