Laravel 5 : [Call to a member function getAction()

2019-02-25 20:36发布

In my Controller, I have

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.

2条回答
霸刀☆藐视天下
2楼-- · 2019-02-25 20:51

You can use this code...

if(!App::runningInConsole()){
    Route::getCurrentRoute()->getAction()['as'];
}

when you run artisan command it will not get an error.

查看更多
姐就是有狂的资本
3楼-- · 2019-02-25 20:55

Seems obvious doesn't it?

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.

查看更多
登录 后发表回答