phpslim v3,检查是否存在命名路由

问题描述 投票:2回答:1

如何检查phpslim v3中是否存在命名路由?我似乎无法对不存在的名称进行尝试/捕获,执行仍然存在,但有错误。

try {
    $redirect_to = $this->router->pathFor($dynamic);
}
catch (\Exception $e) {
    $redirect_to = $this->router->pathFor('dashboard');
}

谢谢

php slim
1个回答
1
投票
try {
    $this->router->getNamedRoute($dynamic);
    echo "Route found.";
} catch (RuntimeException $e) {
    echo "Route not found!";
}

参见:https://github.com/slimphp/Slim/blob/3.x/Slim/Router.php#L268

© www.soinside.com 2019 - 2024. All rights reserved.