I have a Laravel project right now and I'm using Github for my project.
I have two branches, master and develop.
The problem right now is,...
I have all the files in one folder /dev I'm using Sublime Text 2 and the official Github Client. When I switch branches, I see that in ST2 in the status bar, that's fine.
I put sftp-config.json in my gitignore BUT I have different FTP data for master and develop. I always have to edit the data in ST2 tu upload correctly onto my FTP to test my changes. Sometimes I even forget that, and accidentally upload develop to my master/live page.
Same problem for the routes.php, I need to disable SSL in my routes.php for the DEV because I do not have a wildcard certificate and my dev branches/ftp runs on dev.domain.tld and my main site at www.domain.tld .
I created a environment for my Laravel configs, one main config and "development" config.
Is it possible to use Config::get('app.ssl') in my routes.php in my filter? Like that:
Route::group(['before' => ['csrf',Config::get('app.ssl')]], function () {
Route::get('page', array('as' => 'page','uses' => 'PageController#getIndex'));
});
?
My workflow right now is very annoying and confusing sometimes. I always have to check that I do not upload stuff on my live server or changes the master files.
Any suggestions are highly appreciated!
Of course! Laravel supports environmental configuration out of the box. You can find pretty much everything you need in the official docs. However here's an example:
app/config/app.php - "main config"
array(
// other config entries
'ssl' => true
)
app/config/local/app.php - config for the environment local (you can call it whatever you want)
array(
'ssl' => false // here we override the value from our main config
)
Now the last thing we have to do is make sure our environments get detected correctly.
Laravel uses the host name to detect an environment. So you need to figure out how your machine(s) is called.
An easy way to do that is with the PHP function gethostname()
In bootstrap/start.php you can define your environments:
$env = $app->detectEnvironment(array(
'local' => array('your-machine-name'),
'other-environment' => array('other-machine-name')
));
Related
I'm a long time Zend Framework user (now renamed Laminas). But I decided to give a try to last Symfony version. So I just installed it in 5.1.2.
I'm facing a question regarding the multiple environments deployments. In my compay, we have :
Local environment which is developer pc.
Development.
Staging.
Production.
In ZF-Laminas, we have a global.php file which is located in config directory.
For those of you who are not familiar with this framework, you can override key set in global.php file by creating local.php file.
In this global file, I use to put standard configuration for my application.
For example (prod) :
'open_id' => [
'client_id' => 1234
]
Then, I have development and staging files which car override those values for every environmenet. During the deployment, the file corresponding to the environment is copied to local.php.
Let's say staging.local.php.dist becomes local.php with :
'open_id' => [
'client_id' => 5678
]
Which is fine because value is overriding the one from global file.
I would like the same behavior in Symfony but I don't see something similar in Symfony 5.
So far, I only found two possibilities
Create a bundle which will allow me to have a <bundle_name>.yaml file in config/packages directory. According to the documentation (https://symfony.com/doc/current/configuration.html#configuration-files), I will be able to have dev, prod and staging overrides. But it forces me to create a bundle to handle just some standard configurations, which is huge.
Use .env files. But .env files only allow string data, not complex data like arrays.
What do I miss ? Or is it my "zend" way of doing things that is wrong ?
Thanks.
You can also create services_%env%.yaml (services_dev.yaml, services_test.yaml) files for each environment. It will allow you to define different parameters and override/define services for each environment.
Example:
config/services_dev.yaml
parameters:
hello: 'world'
From what I understand from your post, your goal is to have different config values based on the server you are on. If this is the case, you can use environment variables (in the .env file or .env.local for server specific config). You can then use these values in your applications by binding the env var to a parameter. This parameter will then be available within the configuration by using %parameter_name% as value or within the container. You can also pas parameters to services (service definitions are handled the same way as any other config). For more information you can checkout these sources:
https://symfony.com/doc/current/configuration/env_var_processors.html
https://symfony.com/doc/current/configuration.html
I have a simple web app. I have a production database for the live version, and a test database for development. I store the database credentials in a config file, like so:
<?php
return array(
'hostname' => 'myhostname',
'username' => 'myuser',
'password' => 'mypassword',
'dbname' => 'myLiveDB',
);
?>
This file is tracked by git. On the development branch I want to have file with the same name, just with different credentials. I do my work in this branch, test things out and then merge into master when I want to deploy a newer version.
I never want to see this file change during a merge/rebase operation. Each branch should always keep its own version and it should only be updated by committing directly onto the branch.
What I have considered so far:
.gitattributes files with merge=ours for the config file + git config --global merge.ours.driver true. I don't think this works for rebase, which is a problem. I'm also not sure if every other developer working on this would need to update their config manually, or is there a way to change that setting on a repository level?
ditch the config file, rewrite everything to use environment variables. set them manually for the server and for each dev.
have a config_test.php and a config_prod.php in the repository. When deploying, have the script take a look at what branch we are deploying from and copy the the correct file while renaming it to config.php
I guess the third point might be good and a right method. In spring-boot (java) we used to have different profiles in yamal file (application.yml) with names like dev, test, prod etc and spring itself will take the exact file when it is running in that specific environment. Check something like is there or not.
But in your case, just think whether you need a file which is under git or not.
Think that you have a file under your home directory (say /home/myname/testfile/development.properties). And change the code so that, if this properties file exists then take it otherwise take the other one.
I am starting a new project.
I am using ZF2. I have just installed it and have the Skeleton application up and running.
This is my deployment process:
I develop on my local machine
I then push to my public github repository
I then use deployhq.com to deploy those to my production
server which is where the user would see the changes made.
I have tried to look around stack, zend site, and google at blogs etc but still dont have any real understanding or solution to my problem.
I want the application to use different database credentials based on its environment. E.g. if on 'dev', my local machine, then use credentials A, but if on live server, then use credentials B.
I have read a lot about global and local autoload config files etc, but baring in mind my github repo is public, any where I commit any config files with my db details would be visible.
I was wondering if there was a way to have, the same theory, global and local files with the DB connections in, i upload the production details manually, not via git for security reason, and tell git to ignore the local config file somehow? I would also need to know how I tell the application to use those config files based on the environment and there location.
In Zend 2 There are
Global configuration file &
Module level configuration file
IF you want to know there use you can refer the link below
How does configuration works in ZF2
When I had a same scenario I used the above link to understand and exploit Zend Config module which is really good to handle the situation like this .
create two files
production.php
local.php
in both these files
return this array based on the environment
return array(
"dbname" => "yourdbname"
"dbhostname" => "dbhostname"
"dbusername" => "yourdbusername",
"dbpassword" => "yourdbpassword"
);
in config/autoload/ directory of your zend framewrok application
later edit your config/application.config.php file as per below instructions
// get the application env from Apache vhost file ( here you can set in your apache vhost file as production or local )
$applicationEnv = getenv('APPLICATION_ENV');
$environmentSpecificConfigPath = "config/autoload/{,*.}{".$applicationEnv.",local}.php";
// Next with in the config array pass the environment specific configuration path
'config_glob_paths' => array($environmentSpecificConfigPath)
in any controller or action
you can just use the below code
$configArray = $this->getGlobalConfig();
Now $configarray has all your DB credentials to create a connection
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => $configArray['dbname'],
'username' => $configArray['dbusername'],
'password' => $configArray['dbpassword']
));
If you use config array to connect the DB in your entire application
you dont need to worry about environment changes just make sure you have an Apache APPLICATION_ENV entry in your vhost file
you can do that by adding below line in your apache vhost file
SetEnv APPLICATION_ENV "production" // in your production server
SetEnv APPLICATION_ENV "local" // in your local
Also Last but not least you can use the Zend Experts module ZeDB
https://github.com/ZendExperts/ZeDb
To manage your CRUD applications
Hope the above steps may help you in creating the environment
I am trying to set up multiple environments on a Laravel 4 app, and naturally different Databases depending on which environment we are on.
For my local machine, I set up a virtual host for "local.elders.dev"
Unfortunately, for some reason the following code is not working.
$env = $app->detectEnvironment(array(
'local' => array('http://local.elders.dev'),
));
Maybe I need to run an artisan command or something else. I feel I am close, but not quite there yet !
Thanks to all !
Another method is to use the name of the folder the project is in. This works in console and web. I found this to be the only reliable way if different environments are hosted on the same server.
$env = $app->detectEnvironment(array(
'staging' => strpos(getcwd(), '/staging')>-1,
'acceptance' => strpos(getcwd(), '/acceptance')>-1,
'production' => strpos(getcwd(), '/production')>-1,
));
A big advantage is that you can move staging to production by simply renaming or copying the project folder without changing files, db records or environment variables.
I know this is answered but for others looking for a solution...
My environment detection setting looks like this:
$env = $app->detectEnvironment(array(
// Development
// any machine name with the term "local" will use the local environment
'local' => array('*local*'),
// Stage
// any machine name with the term "stage" will use the stage environment
'stage' => array('*stage*')
// Production
// production is default, so we don't need to specify any detection
));
This is handy because it will work on any project as long as I use "local" for development (like "localhost", "localhost:8000", "my.app.local", etc.). Same goes for "stage". And production is default so anything without "local" or "stage" works for production.
Handling multiple environments on the same machine with Laravel 4x
What if you wanted to run multiple environments on the same machine with the same name- for example, a staging and production AND local environment?
There is a better solution for handling environments in Laravel 4x- and it can be done by adding a one liner to you vhosts file- or .htaccess:
Set local environment variable
In vhost or .htaccess add for your local installation, for staging, for example add:
SetEnv LARAVEL_ENV staging
and the same in your production .htaccess or vhost:
SetEnv LARAVEL_ENV production
Then the usual detectEnvironment() function in start.php.
$env = $app->detectEnvironment(function()
{
// Default to local if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: 'local';
});
We didn't forget local... and that's the the cool part-- your installation will default to local if neither environment variable is found in vhost or .htaccess, as they would be found in the other installations.
OK ! I just solved the issue... The code was actually working ok ! The problem is that I was using
$_SERVER['DB1_HOST'] //for Pagodabox.
Of course this was not set on my local environment, which pretty much broke the app...
I fixed by simply doing :
isset($_SERVER['DB1_HOST']) ? $_SERVER['DB1_HOST'] : '';
Thanks to #jeroen and #theshiftexchange :)
Try replacing it with 'local.elders.dev', I'm not 100% sure but it's probably matching hostnames, not full paths.
Laravel 4 detects the environments through the machine names specified in the "bootstrap/start.php" file.
For example, in my case the config becomes:
$env = $app->detectEnvironment(array(
'local' => array('Victor.local', 'Victor-PC'),
));
This means that Laravel will use the 'local' environment settings for both machines: 'Victor.local' (a Mac) and 'Victor-PC' (Windows).
In order to know the current machine name, you can use the following PHP code:
<?php echo gethostname(); ?>
For each environment you can create a folder in app/config and replace the desired configuration files and properties.
Regards!
I sometimes work with Codeigniter, and after I'm done developing on the local site, I need to migrate the files to the server. All the files in the /config/ folder need to be changed to match the Server settings. It's not right to make an entire commit for these changes, do I simply make Git ignore these files altogether, or is there a way to track these changes and apply them with Git at the right moment?
You could keep versioned:
two "value config files", with the right values for each environment
a template config file, with value placeholder in it (for instance, ##PORT_NUMBER##)
a script able to generate the actual config file depending on the current environment
a content filter driver which, on checkout, will trigger the script in order to generate the right config file.
Note: that supposes your template config file has a recognizable content (the filter doesn't have a name or path of the file). See "Git equivalent of subversion's $URL$ keyword expansion" for more on the limitation of git content filter driver).
It depends on Your needs. In my company we use other approach.
We've created several environments (where the asterix is internal project name):
devel - site runs on domain **.local.com*
test - site run on domain test.*.company.com
beta - beta.*.company.com
production - every other domain.
Based on the domain name we switch automatically configurations.
Basicly config file looks like:
<?php
return array(
'_env' => array(
'devel' => array(
// config for devel
),
'production' => array(
// config for production
)
)
);
?>
Some frameworks (AFAIR Zend) set the environment name in Virtual Host config (or .htaccess). You should look at: zend framework auto switch production staging test .. etc
Have You looked at CI documentation? There's a section about it.
Create two folders in the config folder. One is called development and the other is production. Now copy config.php, database.php etc to each of these folders. Now when you are on production server, CodeIgniter will first check the production folder for the files. If it is not there, then it uses the default file in the config folder. And if you are on development environment, CodeIgniter will first check the development folder.
If you want to keep any config file identical to the production and development environment, keep it in config folder.
If you want to set the environment then add the following code in .htaccess file:
#This code for Development Environment
SetEnv CI_ENV development
and
#This code for Production Environment
SetEnv CI_ENV production