Whats is Production enviroment in Laravel? - laravel

I am curious to know what the APP_ENV entry is used for in Laravel environment files. Is it just for my own usage so I can detect it in code? If I create a fresh Laravel app and change APP_ENV to production what will it change under the hood? Nothing?
Thanks!

Typically you would have a different .env file on each server. It is up to you if you would like different parts of your app to work differently in different environments. The .env file is usually used so that your code can just grab values with the env helper function. This way when you change environments like switching from a test API key to a live API key for example, you can just edit the .env file and not touch the rest of your code.
https://laravel.com/docs/5.5/configuration#environment-configuration

Related

Why calling env() function directly in controllers does not work?

Supporting Laravel 6.20 app, I see a lot of env() methods called in the controllers.
It does not work on my side, so I made a wrapper in the config/app.php file, and using it made it work.
And I have to make a lot of similar changes in the app to make it work properly...
But looks like that env() worked for my client on live and developers server.
I know that calling env() in controllers directly is a bad way but wonder why it worked on servers?
Are there some common PHP configurations?
Thanks!
you have probably cached your configurations by running php artisan config:cache
according to the doc
This command will combine all of Laravel's configuration files into a single, cached file, which greatly reduces the number of trips the framework must make to the filesystem when loading your configuration values.
and then
If you execute the config:cache command, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function for .env variables will return null.
you have to just run php artisan config:clear and this will clear the cache file. you will be able to get the env values again.
but yeah don't use any values directly from .env files. there are some issues. to get more performance, cache the configs. so wrap the env value in a config key like in config/app.php
'name' => env('APP_NAME', 'Laravel')
you are saying that use the APP_NAME key from the env file as your laravel app name and if there's no APP_NAME key or value in env use Laravel as default.
secondly if you make some requests within a very short period of time, like page loads and you are calling some xmlhttp requests at the same time, there's possibility of not getting the env values (returns null) if you have to use any. this is a php bug. so read env values from config files with a default value for performance and for avoiding bug.

Laravel configuration usage for different servers in pure php file

I am new in Laravel and want to know such problem
I am running an web application on several server using Laravel.
But I have encountered with an issue.
When there is modification for the project, I need to sync with git on several servers.
But it has different settings for each server (eg: DB name, DB password...)
I have set it manually because I couldn't use .env or configuration file since the file is just pure php file.
The issue I want to solve is how can I get Laravel configuration data from pure PHP file(not controller or whatever).
It will be thankful if someone teach me solution.
You asked:
how can I get Laravel configuration data from pure PHP file
The answer would be this:
You just make some file in config/ folder of laravel app (or use the existing file like config/app.php)
You make an array of your key value pairs
<?php
return [
'some_key' => 'some_value',
...
and you simply call it with this code where ever you need it:
config('config_file.key');
for example
config('app.name');
would give you Laravel by default.

Checking a remote url depending upon config

I am a new NativeScript user and I am trying to understand how to have my app make a GET call to a remote server, depending upon environment. In Java world we pass in an environment variable, but I have not found an example (that I understand) demonstrating how my NativeScript mobile app will know which environment it is running in, and how to get values based upon that.
I am presuming that I will have a config/ with files such as
prod.conf.js and dev.conf.js - and there is where I will put in my urls and other config values.
How do I get my NativeScript (which I will eventually build into iOS) to reach those values to use in an http request upon startup. Any example to direction to documentation would be greatly appreciated.
Answer:
I finally got the problem solved by using Manoj suggestion of nativescript-dev-appconfig. For any newbie looking for help, try this:
{PROJECT_ROOT}/config - create the environment files as {env}.json. These files are just json, with name-value pairs of the values you want. In may case that is
When you do your build: tns build ios --bundle --env.config {dev | test | prod }
This will take the contents of the selected env.config file (ie -env.config dev and copy it as - {PROJECT_ROOT}/app/config.json .
To use it within your code,
import config from "../config.json";
axios.get(config.MY_URL).then(result => {...}
Unfortunately using environment based config files are not officially supported but there is a plugin hook you could try.
We also have webpack based environment variable support, read more about that in the docs.

Setting Config Variables Permanently

I was trying to save my configration variables permanently. For example app.debug i want to change dynamically in my control panel and save it permanently.
I tried lot of packages but not supporting laravel 5.2 or i cant:
https://github.com/Phil-F/Setting/
https://github.com/daftspunk/laravel-config-writer
https://github.com/anlutro/laravel-settings
When I try to overwrite, its going to more big problem because some variables need to get in to environment. How can i do this?
Thank you..
Okay,
I coded a new package for Laravel 5.x and i can make permanently config variable with this code below
Permacon::set("config", "locale", "tr");
Permacon Package => https://github.com/furkankadioglu/Permacon

How should I store my api key on heroku so that it still stays secret instead of embedding in web page code

I have built a flak app, locally I have stored it as environment variable, but I do not know how should I store my api key on heroku so that it still stays secret instead of embedding in web page code ?
Any help will be greatly appreciated.
You can store your api-key as environment variables as these are perfectly secure:
go to you local folder and run heroku config:set key_one=value_one key_two=value_two and more.
Note: run above commands in the same folder which points to your repository.
once you set the environment variable you can access this key value pair in you code directly as:
var api_key = process.env.key_one;
You can also easily add or edit your config variables from your heroku app’s Settings tab, look here at Heroku's official documentation.

Resources