In laravel I need to create variable in env file. According to documentation and some relevant threads (got after googling) I need to do add variables in .env file
I have tried following
SANDBOX_PAYPAL_CLIENT_ID=7I3D9
SANDBOX_PAYPAL_CLIENT_SECRET=S2E4C5R6E7T
I am confused that after adding here variable I just need to call then into controller or I need to setup again in somewhere ./config/ directory? What is best practice, can someone guide me about that. I would like to appreciate. Thank you
You should put env variables in .env file, but make sure to only use env() calls in configuration files (the ones under /config) to retrieve their values.
Keep in mind that if you cache the configuration (php artisan config:cache) any env() will return null as Laravel will no longer load .env files.
That's also stated in to the documentation
You can get .env variables using the env() helper function.
Most packages get these values in their conf file to group the configurations.
You can then use the config() helper function to get the value.
However you implement this is up to you.
It depends on how you want to use them. Maybe create a file with name ConfigVariables.php declare all variables of env there.
1) You can have all of them at one place. Centralized control of all env variables at one place.
2) Take care of null checks etc, if config doesn't exit, pass some default value
3) This will help you to have one standard being used for all
4) If you use env directly in the code, you will have to check and take care of the null values (in case if there is no variable in the config)
Just to add a point here, Strings with spaces could be an issue if you do not use ""
You can test it:
Email_Sender_Name=Danyal Sandeelo
echo it as: echo env("Email_Sender_Name");
Email_Sender_Name="Danyal Sandeelo"
echo it as: echo env("Email_Sender_Name");
You can read this one: Laravel 5.2 not reading env file
Related
I have an .env file containing SOME_IP=127.0.0.1:8080. In a Laravel controller for a certain request I call $foo = getenv('SOME_IP');. About 90% of the time it works fine, I get the string and proceed. But the other 10% of times, getenv returns false, even though the variable is clearly in the .env file. What could be causing this?
Alternatively, Laravel's env returns null.
Observed with vlucas/phpdotenv v2.6.4.
Maybe it's because of caching the config.
It is better to load your env variable in a config file and then retrieve it from the config.
For example you can create a file with the name 'ips' in config directory like this:
<?php
return ['someIp' => env('SOME_IP')];
and then use
$foo = config('ips.someIp')
I added this to my .env:
PHOTOS_DIR=%kernel.project_dir%/var/photos
Of course, when I try to retrieve the value of $_ENV['PHOTOS_DIR'], I get the raw string %kernel.project_dir%/var/photos.
But how can I get the value processed by the Symfony config processor, e.g. /my/project/var/photos?
EDIT:
I'm aware that it is simply possible to add this in services.yaml:
parameters:
photos_dir: '%kernel.project_dir%/var/photos'
But I would like to keep important config data in the .env file.
In order to expand %kernel.project_dir%, use %env(resolve:...) in parameters, e.g.:
parameters:
photos_dir: '%env(resolve:PHOTOS_DIR)%'
Your dotenv does not process the symfony yaml processor parameters.
Delete the %kernel.project_dir% from .env file and in your .yaml you need to bind a parameter like this:
photos_absolute_path: '%kernel.project_dir%%env(PHOTOS_DIR)%'
After that you can get the parameter from your container by the name of photos_absolute_path and it will point to the correct location
Using symfony parameters in .env-files is correct. Symfony will automatically resolve them.
After container will be compiled, you can get the real value if you bind it to a parameter.
.env
PHOTOS_DIR=%kernel.project_dir%/var/photos
services.yaml:
parameters:
photos_dir: '%env(PHOTOS_DIR)%'
Somewhere in your application:
$container->getParameter('photos_dir');
I have, let's say, five types of job A,B,C,D,E. Each has different configuration. I want to make a configuration system in PHP, which is solely in PHP. In this system I like to return an object of configuration on specifying job type:
$obj=new ServiceConfiguration(); $configForA= $obj->getConfig('A');
getConfig reads the config, which may contain URL, job server, port and set this in config object. In writing all this I want that proper design should be followed.
Should I write all config parameter in a PHP array in a config file or there is any better alternative to it? Can I use external parties like YAML to store config? I want to know the best way to achieve it. Please answer with the whole design.
NOTE: I want to write a third party which should be written in PHP and should have no or minimal dependency on external third parties like YAML.
I personnaly use JSON files for configuration and a PHP class to read thoses files.
Accessing a var is doing by giving a string with underscores, that way I can explore the depth of the JSON file
example :
database.json
{
"prod" :
{
"server" : "127.0.0.1"
},
"key_with_underscore" : "value"
}
script.php
Config::get('database_prod_server');
Config::get('database_key_with_underscore');
I also do some small treatment to access JSON array as PHP array but basically, that's it.
In fact the config class just transform a multidimensionnal array to one-dimensionnal array by flattening it and insert an underscore for each new "dimension", then I stock the result in a variable (and cache it).
I want to know a clean way of defining Application Constants in Codeigniter. I don't want to change any native file of codeigniter. Hence I don't want to define it in application/config/constants.php as when I need to migrate to newer version of code-igniter I will not be able to copy the native files of codeigniter directly.
I created a file application/config/my_constants.php and defined my constants there. 'define('APP_VERSION', '1.0.0');'
I loaded it using $this->load->config('my_constants');
But I am getting a error
Your application/config/dv_constants.php file does not appear to contain a valid configuration array.
Please suggest me a clean way of defining application level constants in code-igniter.
Not using application/config/constants.php is nonsense! That is the only place you should be putting your constants. Don't change the files in system if you are worried about upgrading.
just a complete answer. (None of the answers show how to use the constants that were declared)
The process is simple:
Defining a constant. Open config/constants.php and add the following line:
define('SITE_CREATOR', 'John Doe')
use this constant in another file using:
$myVar = 'This site was created by '.SITE_CREATOR.' Check out my GitHub Profile'
Instead of using define(), your my_constants.php file should look something like this:
$config['app_version'] = '1.0.0';
Be careful with naming the array key though, you don't want to conflict with anything.
If you need to use define(), I would suggest doing it in the main index.php file, though you will still need to use APP_VERSION to get the value.
config file (system/application/config/config.php) to set configuration related variables.
Or use
constant file (system/application/config/constants.php) to store site preference constants.
=======================
DEFINE WHAT YOU WANT
=======================
$config['index_page'] = 'home';
$config['BASEPATH'] = 'PATH TO YOUR HOST';
Please refer this:
http://ellislab.com/forums/viewthread/56981/
Define variable in to constants & add value on array
$ORDER_STATUS = array('0'=>'In Progress','1'=>'On Hold','2'
=>'Awaiting Review','3'=>'Completed','4'
=>'Refund Requested','5'=>'Refunded');
You can accomplish your goal by adding constants to your own config file, such as my_config.php.
You would save this file in the application/config folder, like this:
application/config/my_config.php.
It is very common to have a separate config file for each application you write, so this would be easy to maintain and be understood by other CI programmers.
You can instruct CI to autoload this file or you can load it manually, as needed. See the CI manual on "Config class".
Let me suggest that you use composer.json to autoload your own Constants.php file, like this:
I have a project based on codeigniter. And I should use one class that extended from a codeigniter controller in another php file. But I didn't find the solution about how to teach another php file to see whole CI-project. Beyond that needed class can not inherit when i call it from other place.
I'm not 100% sure if this helps get you in the right direction, but kudos if it does!
Codeigniter routes the application depending on the environment state of the URI. What you need to do is set the environment and include the index view file like so:
$_SERVER["REQUEST_URI"] = "cms/2";
//Set GET action,method params etc
require_once "path/to/index.php";
When you load CI Index file it reads the SERVER Variable and others which you may have to find and execute the controller and method, I would also advise that you modify the library/view file as it may exit upon output causing your script to exit.
Also you may wis hto look into ob_start() to catch the buffer.