Ad

Laravel - $_SERVER['DOCUMENT_ROOT'] Empty When Running Queued Jobs?

- 1 answer

In some of my files I have these sorts of lines:

require_once($_SERVER['DOCUMENT_ROOT'].'/../vendor/stripe/stripe-php/init.php');

Usually all is well. Now I am running some queued jobs, and I am seeing errors of the sort:

Failed opening required '/../vendor/stripe/stripe-php/init.php' (include_path='.:/usr/local/php5/lib/php') in /Library/WebServer/Documents/mysite/app/Somemodel.php:9

So it is as if when things are executing from artisan $_SERVER['DOCUMENT_ROOT'] is empty. I am on a MAC with Sierra. Maybe there is some setting that I overlooked?

Ad

Answer

Queued jobs are running in the background so $_SERVER['DOCUMENT_ROOT'] is empty, because it's a path retrieved from the web server directive

It also explains why it's empty when running the Artisan CLI

You can require the file using the base_path instead

require_once base_path() . '/vendor/stripe/stripe-php/init.php';

Here's the difference in Artisan Tinker

~/Sites/laravel (master ✗) ✹ ★ ᐅ  tinker
Psy Shell v0.9.9 (PHP 7.3.9-1~deb10u1 — cli) by Justin Hileman
>>> require_once base_path() . '/vendor/stripe/stripe-php/init.php';
=> true
>>> require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/stripe/stripe-php/init.php';
PHP Fatal error:  Failed opening required '/vendor/stripe/stripe-php/init.php' in Psy Shell code on line 1
>>> $_SERVER['DOCUMENT_ROOT']
=> ""
>>> base_path()
=> "/home/caddy/Sites/laravel"
Ad
source: stackoverflow.com
Ad