Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the laravel .env variables inside itself

Tags:

php

laravel

I need to ask stupid question but my question to access .env variable inside it self not from php :

If i have .env file for larvel5.4 and i have APP_URL Like this :

APP_ENV=local
APP_KEY=base64:7qLJMqTxrAPk+tLJscVlmrzf2H16tAfbSoCZuleCkxQ=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

and i have multi config variable use the domain link like this :

#Facebook

FACEBOOK_LOGIN_URL=http://localhost:8000/en/portal/facebook_login
FACEBOOK_CALLBACK_URL=http://localhost:8000/en/portal/facebook_callback

#Twitter

TWITTER_LOGIN_URL=http://localhost:8000/en/portal/twitter_login
TWITTER_CALLBACK_URL=http://localhost:8000/en/portal/twitter_callback

#Google

GOOGLE_LOGIN_URL=http://localhost:8000/en/portal/google_login
GOOGLE_CALLBACK_URL=http://localhost:8000/en/portal/google_callback

is there way to access the APP_URL in the same file like this :

FACEBOOK_LOGIN_URL= APP_URL /en/portal/facebook_login

Please i am new member don't give me minus for this question.

Thank you all

like image 876
moheb hawari Avatar asked Apr 25 '17 11:04

moheb hawari


People also ask

Can I use variables in .env file?

You can set default values for environment variables using a .env file, which Compose automatically looks for in project directory (parent folder of your Compose file).

How do I change the variable in an env file?

A simple way to update the . env key value in laravel is to add the below code in the controller function where you want to change . env values. $key = 'VARIABLE_NAME'; $value = 'NEW VALUE'; file_put_contents(app()->environmentFilePath(), str_replace($key .


3 Answers

You can keep things simple, while accessing ENV variables you can easily do the following:

Env file:

APP_URL=http://localhost:8000
FACEBOOK_LOGIN_URL=/en/portal/facebook_login
FACEBOOK_CALLBACK_URL=/en/portal/facebook_callback

in Laravel:

env('APP_URL') . env('FACEBOOK_LOGIN_URL');

And Yes we can do that if needed use following syntax:

.env file:

APP_URL=http://localhost:8000
FACEBOOK_LOGIN_URL=${APP_URL}/en/portal/facebook_login
FACEBOOK_CALLBACK_URL=${APP_URL}/en/portal/facebook_callback
like image 168
Yogesh Koli Avatar answered Oct 22 '22 14:10

Yogesh Koli


In .env file:

APP_URL=http://localhost:8000
FACEBOOK_LOGIN_URL="${APP_URL}/en/portal/facebook_login"
like image 34
radzi0_0 Avatar answered Oct 22 '22 13:10

radzi0_0


While other answers are correct about using variables stored in .env. I think it's going to be neater, if you do the following:

url(env('FACEBOOK_LOGIN_URL'))

or:

url(env('FACEBOOK_CALLBACK_URL'))

url() uses APP_URL so you don't need to concatenate your .env variables.

like image 1
Ivanka Todorova Avatar answered Oct 22 '22 13:10

Ivanka Todorova