Tiago Peres
Tiago Peres

Reputation: 15421

Run Laravel command dependent on environment variables

Generally speaking when wanting to see the routes locally I'd use

php artisan route:list

That worked fine when I wasn't dealing with files that use environment variables which are declared differently between my local environment and the server. Now I get

PHP Notice: Undefined index: VARIABLE_NAME in ....php on line 12

Notice: Undefined index: VARIABLE_NAME in ....php on line 12

In particular, in the server I use

if (!defined('VARIABLE_NAME')) { define('VARIABLE_NAME', $_SERVER['VARIABLE_NAME']); }

$variable = env('VARIABLE_NAME') ? env('VARIABLE_NAME') : VARIABLE_NAME;

Basically the difference between the server and the local environment would be the following part (which locally I don't use and is where the conflict is at because there's no $_SERVER['VARIABLE_NAME']))

if (!defined('VARIABLE_NAME')) { define('VARIABLE_NAME', $_SERVER['VARIABLE_NAME']); }

Tested other variations too, like

if (!defined('VARIABLE_NAME') or !getenv('VARIABLE_NAME')) { define('VARIABLE_NAME', $_SERVER['VARIABLE_NAME']); }

I could change the change the code locally and ignore the file. Yet this is a file that can change frequently, so that's not ideal.

In light of the goal (see the route list), what would be the most convenient way? By convenient I mean without having to declare the variables one in the shell or having to mantain different files for both local and server.

Upvotes: 1

Views: 669

Answers (1)

Tiago Peres
Tiago Peres

Reputation: 15421

Based in this answer, before running

php artisan route:list

I'd run

set -o allexport
source .env
set +o allexport

and then was able to see the routes just fine

enter image description here

Upvotes: 1

Related Questions