我有一個問題:對於這樣的例子,我有一個symfony3應用程序,它有以下途徑:/admin/login
,admin/news
,admin/gallery
,但路線/admin/authentification
不存在。所以這個想法是,如果路線不存在,我想將用戶重定向到主頁/
。你能幫我嗎 ?在此先感謝和抱歉,我的英語重定向如果路線does'n存在
1
A
回答
0
我不相信這是最好的解決,但你可以使用一個UrlMatcher
來檢查URL你傳遞關聯到一個可用的路線:
/**
* @Route("/debug")
*/
public function DebugAction(){
$router = $this->get('router');
//Get all the routes that exist.
$routes = $router->getRouteCollection();
$context = $router->getContext();
$urlMatcher = new UrlMatcher($routes, $context);
$url = '/admin/login';
try{
//UrlMatcher::match() will throw a ResourceNotFoundException if the route
//doesn't exist.
$urlMatcher->match($url);
return $this->redirect($url);
} catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e){
return $this->redirect('/');
}
}
我對這個解決方案並不特別熱衷,因爲它依賴於捕獲異常,而不是檢查布爾值來確定路由是否存在。
0
您可以檢查路由是否存在。
function routeExists($name)
{
// I assume that you have a link to the container in your twig extension class
$router = $this->container->get('router');
return (null === $router->getRouteCollection()->get($name)) ? false : true;
}
並根據結果,做重定向到路由或默認網頁,或任何你需要的。
相關問題
- 1. 重定向到主頁如果路線不存在Laravel 5
- 2. 重定向路線
- 3. 重定向如果cookie存在jquery/html
- 4. 重定向某頁如果*不存在?
- 5. 如果cookie存在,Nginx重定向
- 6. 重定向如果會話不存在
- 7. .htaccess如果文件存在重定向
- 8. 重定向路線 - 什麼是燼路?
- 9. 在新的Angular2路線中重定向
- 10. 如何重定向路線(使用通配符),但如果找不到重定向,轉到默認路徑?
- 11. 如何更改路線值然後重定向到該路線?
- 12. Opencart重定向定義的路線
- 13. 在BrowserSync中,如何匹配特定路線並重定向?
- 14. 重定向如果在.htaccess
- 15. 應用路線 - 不重定向到
- 16. Rails的路線,重定向腳本
- 17. 重定向傳統路線呈現耙
- 18. Ember測試路線重定向
- 19. 從所有Ember路線重定向
- 20. 重定向路由嚴格的斜線
- 21. 「子路線」永久重定向
- 22. Laravel路線()重定向到http:// localhostarticle
- 23. Angular - 從JavaScript重定向到路線
- 24. Rails路線重定向與符號
- 25. 重定向路線編程方式使用路線名稱
- 26. 檢查div是否存在,如果不存在則重定向
- 27. 如果文件存在(緩存),在.htaccess中重定向
- 28. 如果設計存在限制路線
- 29. FlowRouter重定向如果用戶登錄,如果路徑是
- 30. 的.htaccess重定向如果文件存在,那麼重定向回
可能重複[重定向所有事件監聽器「找不到路由404未找到 - NotFoundHttpException」](http://stackoverflow.com/questions/26464624/redirect-with-event-listener-for-all-no -route-發現-404未找到-notfoundhtt) – chalasr