Laravel: Join two application by one config - php

My project is two Laravel applications in one folder. Why two separate applications instead of one? I have good reasons. Anyway, for example, every time I want to change the app name in the config (config/app.php) I go and edit the .env file in both directories (of the two applications) and then execute artisan config:cache to update the cached config.
It's not much of a task or anything, but it's a bad design I believe. So, I am looking for a way to take the values shared between these two application out to a separate file. And have these application load this file and override/apply the values inside it to the app configuration.
So, now I have a config.php in the root directory (which holds the other two apps in /app1 and /app2). And inside each app's AppServiceProvider I call the config.php and loop through the values and set each using Config::set(..., ...).
This worked for me well, but of course changing the values loaded in the \Config package doesn't change the values that were fetched from it earlier to this point. For example app()->environment() returns the value set in the config/app.php, not the new value introduced in the global config.php. That's because the App library asked for the env property before I could override it!
So, what I want your help with is: you either tell me about a more smart/standard way to achieve 1 config for 2 apps setup. Or tell me where to put the code that overrides the app config with the new values from my global config.php (currently I put the code in register() in App\Providers\AppServiceProvider)

You can share env settings by removing one of .env files and make a symlink to the other file instead of a common file. In this case if you change a file, both applications get changed values.

Related

Laravel 5: How to enable environment based public resources like css, js, images

we have built a multi environment application over laravel 5 with environment based configurations and views, now the problem arises when we try to use environment based public resources like css, js and images, as they are in public directory and does not recursive merge things like in configurations file. can we somehow control it with environment settings etc.
for example:
I have two domains with almost same functionality but differs in some configuration and design, like for example site_1 shows header navigation links on the top while the site_2 does not show header navigation links in top so we can somehow manage it in the configuration site_1.config.settings.header and set values to on or off.
well as per my understanding you are using multiple environment files which varies deployment to deployment, in larvel you can override enivronment variables from environment files and can use one main environment file to override variables like in .env file you can define the APP_ENV = site_1 and can create new environment file named .site_1.env and override all the environment variables which are different for that deployment.
about the public resources you can do the sort of same alike configuration scheme but as you know these files do not recursive merge so you can create a same file in the public/site_1/filename.ext and when adding these files in your templates you can append the environment name as directory to lookup for those files.
You can use the theme package. Like
Cartalyst Theme

In CakePHP, how can I move the database.php config file outside of the APP folder

I'm trying to prevent storing passwords and security information in code files. I've managed to move the Security.cipherSeed and Security.salt into Config files outside the application directory with the following placed in core.php:
App::uses('PhpReader', 'Configure');
Configure::config('default', new PhpReader('/etc/');
Configure::load('config.php', 'default');
In the above however, I can only process key value pairs, so how do I load the database config file from a file outside the entire application? I have tried using require_once('/etc/database.php') in core.php but it's not having the desired effect.
In a short summary:
Create config/<filename>.php.default
Add config/<filename>.php to .gitignore
Use a deployment script to copy config/<filename>.php.default to config/<filename>.php when doing a deployment. This can be done using composers events. Just execute a php script that will do this task for you.
Use env vars to set your actual config config values without editing the files
This will avoid that any "personal" customized config goes into the repository but keep files with default settings in the repository. The env vars are also a pretty convenient way of setting the DB connection up for example.

How do I use one config key in another?

In Laravel 4's cascading configuration files I was able to reference one configuration key from another. For example, I liked to have app.debug set to true or false, and then I could switch error reporting to Sentry to the opposite of that in a different configuration file, by setting the relevant setting to !Config::get('app.debug').
I knew this was a little hairy because presumably whether the config value can be found depends on the order in which files are loaded. But I managed to get it working each time.
I'm having no such luck in Laravel 5. I get a "class not found" error when I try to do Config::get(...) in a configuration file. Using the config(...) helper instead produces no error but the values aren't retrieved.
The convention, of course, is to use environment variables, the .env file and env(...) to grab the values. That way each configuration key can use the value of the environment variable. That's fine and works, but I have some values which are shared between environments, and I do want some of these values to be committed to version control.
Using the .env.example file isn't a good solution for me since I don't want to have to remember to copy values over to the .env file of each environment if they change.
How can I store some configuration values in a way where they are shared between environments, are present in version control, and can be referenced in multiple configuration files?
This is my solution, which works, but I'm interested in other possibilities.
Add a new shared file, checked in to VCS, called .env.shared. Its structure is just like .env. Edit bootstrap/app.php to load in these variables (in addition to .env):
// Load shared environment variables
$app->afterLoadingEnvironment(function () use ($app) {
Dotenv::load($app->basePath(), '.env.shared');
});
I added this right after $app is instantiated.

How to get config data in laravel in a subfolder

I have the Laravel Administrator Plugin and I set up a setting administrator page that is located in:
app/config/administrator/settings/site.php
The application can store the data, but when i try to get some data in one of my controllers like this:
Config::get('administrator.settings.site')
I can get a returned value... Always null or array 0.
How I can get those configuration values?
Solution:
You can use a file path rather than dot notation for file paths:
Config::get('administrator/settings/site.variable_name');
Some Explanation
Dot notation is used to get variables inside the config array within a file, rather than to denote file paths.
Therefore you can use Config::get('administrator/settings/site.variable_name'); to get $variable_name from administrator/settings/site.php, or ENV_DIR_NAME/administrator/settings/site.php depending on your environment.
Directories within in app/config are read as environments. For example app/development/database.php will work with Config::get('database.whatever') when you're in your "development" environment while app/production/database.php will be read with Config::get('database.whatever') when you're in the "production" environment.
Laravel falls back to the config/config/*.php file when no environment-specific file is present.
Note
I believe that admin package has a config file in app/config/packages/frozennode/administrator/administrator.php which the package would like you to use for it's settings. That should be available using this convention: Config::get('package::file.option');
I ran into a very similar problem when using this Laravel-Excel package.
The solution I needed was slightly different to the solution above. I needed to take advantage of Laravel's config overriding feature to have one config setting for normal execution and a different one for testing. Specifically, I normally wanted Excel files to be stored in storage/excel and I wanted them to be put in storage/testing/excel under testing.
The technique of using a slashed path didn't work because it points to a specific file so didn't respect the different environments:
$directory = Config::get('packages/maatwebsite/excel/export.store.path');
The solution was to use a package prefix so that the config loader would respect the environment:
$directory = Config::get('excel::export.store.path');
I'm not exactly sure where the excel shorthand name comes from but I suspect it's something to do with the Excel facade in the package exposing itself as 'excel'.

Loading Config and Setting a Config Value in CodeIgniter

I try to create application with back-end and frond-end. The folder structure as follows(only show config file position to explain problem)
+root
|-admin
|-application
|—backend
|—config
|—config.php
|—frontend
|—config
|—config.php
|-system
Using back-end admin panel I try to change config value in both back-end and frond-end config.php value. But I fail to change it in front-end config.php file. I don’t need database based solution for this.
Reading the documentation I got this:
Note: CodeIgniter always tries to load the configuration files for the current environment first. If the file does not exist, the global config file (i.e., the one in application/config/) is loaded. This means you are not obligated to place all of your configuration files in an environment folder − only the files that change per environment.
So based on that information, I think the problem is when you are calling it, maybe a problem with your path.

Categories