使用路由时无法识别Laravel getPath():list

时间:2015-05-24 08:57:09

标签: php laravel laravel-4 laravel-5 artisan

当我通过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()

相同

有人知道这里发生了什么吗? 非常感谢!

1 个答案:

答案 0 :(得分:1)

发生错误是因为当您在控制台中时,Route::getCurrentRoute()会返回null值。如果您在浏览器中,它将返回当前路线。对此的一个解决方案是在检索其某些属性之前检查当前路由是否为空:

$currentRoute = Route::getCurrentRoute();

if ($currentRoute) 
{
    $path = $currentRoute->>getPath();
}