我有一個問題:對於這樣的例子,我有一個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. 如果cookie存在,Nginx重定向
- 3. .htaccess如果文件存在重定向
- 4. 重定向如果cookie存在jquery/html
- 5. 在新的Angular2路線中重定向
- 6. 如何更改路線值然後重定向到該路線?
- 7. 重定向路由嚴格的斜線
- 8. 重定向傳統路線呈現耙
- 9. Rails的路線,重定向腳本
- 10. Ember測試路線重定向
- 11. 「子路線」永久重定向
- 12. 重定向路線編程方式使用路線名稱
- 13. 檢查div是否存在,如果不存在則重定向
- 14. FlowRouter重定向如果用戶登錄,如果路徑是
- 15. 如果設計存在限制路線
- 16. Symfony重定向搜索路由如果路由未找到
- 17. htaccess重寫,如果重定向文件存在
- 18. 定義角路線根據路由參數重定向
- 19. .htaccess重定向到緩存文件(如果存在)
- 20. 路徑重定向
- 21. 路由重定向
- 22. 自定義重定向路徑如果註冊失敗Laravel 5.3
- 23. 重定向到URL,如果
- 24. 重定向如果發現
- 25. 如何重定向到路線如交易/查看/ {編號}
- 26. 如果子域不存在,我該如何重定向請求?
- 27. 如何使用.htaccess如果文件路徑是* .css重定向
- 28. Sails.js重定向到其他航線路線
- 29. 如何重定向綁定路徑?
- 30. 角 - 重定向如果是的話我指定我的各種頁面,如在路線
可能重複[重定向所有事件監聽器「找不到路由404未找到 - NotFoundHttpException」](http://stackoverflow.com/questions/26464624/redirect-with-event-listener-for-all-no -route-發現-404未找到-notfoundhtt) – chalasr