当我通过cmd执行php artisan route:list
命令时,我正在使用Laravel 5并注意到一些奇怪的事情。
它给了我一个错误:
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to a member function getPath() on a non-object
它所引用的代码是:
Route::getCurrentRoute()->getPath()
但是当我转储该代码时,没有抛出任何错误并正确显示当前路由。
运行php artisan serve
时也没有问题。使用php artisan route:list
命令时发生错误。与Route::getCurrentRoute()->getUri()
有人知道这里发生了什么吗? 非常感谢!
答案 0 :(得分:1)
发生错误是因为当您在控制台中时,Route::getCurrentRoute()
会返回null
值。如果您在浏览器中,它将返回当前路线。对此的一个解决方案是在检索其某些属性之前检查当前路由是否为空:
$currentRoute = Route::getCurrentRoute();
if ($currentRoute)
{
$path = $currentRoute->>getPath();
}