Har*_*old 7 php routing symfony
if ($u = $this->generateUrl('_'.$specific.'_thanks'))
return $this->redirect($u);
else
return $this->redirect($this->generateUrl('_thanks'));
Run Code Online (Sandbox Code Playgroud)
_specific_thanks
当它存在时我不想重定向到url.那么如何检查网址是否存在?
当我这样做时,我有这个错误:
路径"_specific_thanks"不存在.
Ahm*_*ani 11
我认为没有直接的方法来检查路线是否存在.但是你可以通过路由器服务寻找路由存在.
$router = $this->container->get('router');
Run Code Online (Sandbox Code Playgroud)
然后,您可以获取路由集合并调用get()
给定路由,如果该路由不存在,则返回null.
$router->getRouteCollection()->get('_'. $specific. '_thanks');
Run Code Online (Sandbox Code Playgroud)
getRouteCollection()
在运行时使用不是正确的解决方案.执行此方法将需要重建缓存.这意味着将在每个请求上重建路由缓存,使您的应用程序比所需的慢得多.
如果要检查路由是否存在,请使用try ... catch构造:
use Symfony\Component\Routing\Exception\RouteNotFoundException;
try {
dump($router->generate('some_route'));
} catch (RouteNotFoundException $e) {
dump('Oh noes, route "some_route" doesn't exists!');
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4894 次 |
最近记录: |