Laravel 5:[在非对象上调用成员函数getAction()

Laravel 5:[在非对象上调用成员函数getAction()

问题描述:

在我的控制器中,我有

Route::getCurrentRoute()->getAction()['as']

一切都在浏览器中运行良好,但是只要我键入

Everything works well in the browser but as soon as I type

php artisan route:list

在终端机中我有这个异常

in the terminal I have this exception

[Symfony\Component\Debug\Exception\FatalErrorException]
Call to a member function getAction() on a non-object

如果我对此行发表评论,一切都会很好.

If I comment this line everything works well.

似乎很明显吗?

在浏览器中获取当前路线将返回当前访问的路线.在终端中,您没有这样的请求.当询问访问哪条路线时,Laravel将返回null.您必须在调用getAction之前检查返回值.

Get current route in a browser will return the currently visited route. In the terminal you do not have such a request. Laravel will return null when asking what route is visited. You would have to check for the return value before calling getAction.