Reputation: 599
I have some code that can be called from either a web-server or from Tinker/a worker/a script. The code needs to determine whether is is being called from a web-server or not.
I initially thought request() would be null when there's no request, but this is not true. Next I thought request getHost or request getURI would return null if there's no request, but that is not true either.
Is there an elegant way of determine whether the code is running in response to a request? I could use middle-ware, but that seems unnecessarily complicated as well.
The best solution I could come up with is checking if $request->header('User-Agent') contains "Symfony". This seems quite hacky.
Upvotes: 3
Views: 450
Reputation: 18197
You can use app()->runningInConsole()
to check whether the request was received via the web server or through the console.
Upvotes: 4