繁体   English   中英

当 Route::fallback 存在时检查路由是否存在

[英]Check if route exists when Route::fallback present

我试图确定在 Laravel 的Route::fallback内部时是否存在给定的Route::fallback 以下代码段应该抛出NotFoundHttpException异常,但是我怀疑这不是由于存在后备(即,路由确实作为后备存在)。

Route::fallback(function () use ($routes) {
    try {
        $next = Request::create('does-not-exist');
        Route::getRoutes()->match($next);
    } catch (NotFoundHttpException) {
        // Should end up here
    }
});

对于某些情况:我希望能够点击确实存在的路线,然后显示另一条路线的内容。 第二条路线最终可能实际上并不存在,我希望能够抓住它。

TIA

Route::getRoutes()->match($next); 将是一个Illuminate\\Routing\\Route对象。 您可以检查此对象的isFallback属性的值。

Route::fallback(function () use ($routes) {
    $next = Request::create('does-not-exist');
    $route = Route::getRoutes()->match($next);
    if ($route->isFallback) {
        // do your thing
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM