Is it possible to have different database for each module? - laravel-9

I have 2 modules. App1, App2.
Is it possible to define the default connection for different databases in each module?
App1 module 'default' => env('DB_CONNECTION', 'App1')
App2 module 'default' => env('DB_CONNECTION', 'App2')
similar to how there is config/database.php so that when you run module:migrate App1 only the migration in module App1 with the App1 database works?
There is a Config/config.php folder inside each module, is it possible to configure there? and How do you do?

Related

Where to store globally accessible variables using Dancer

I'd like store variables in a configuration file so that I can just set them in one place and access them whenever needed.
Where is the place to put such variables in a Dancer app, and how do I access them?
The best way is to have one config.yml file with default global settings, like the following:
# appdir/config.yml
logger: 'file'
layout: 'main'
Your Question: How to access?
Ans: A Dancer application can use the 'config' keyword to easily access the settings within its config file, for instance:
get '/appname' => sub {
return "This is " . config->{appname};
};
This makes keeping your application's settings all in one place simple and easy - you shouldn't need to worry about implementing all that yourself.
You may well want to access your webapp's configuration from outside your webapp. Using Dancer, you can use the values from config.yml and some additional default values:
# bin/script1.pl
use Dancer ':script';
print "template:".config->{template}."\n"; #simple
print "log:".config->{log}."\n"; #undef
Note that config->{log} should result undef error on a default scaffold since you did not load the environment and in the default scaffold log is defined in the environment and not in config.yml. Hence undef.
If you want to load an environment you need to tell Dancer where to look for it. One way to do so, is to tell Dancer where the webapp lives. From there Dancer deducts where the config.yml file is (typically $webapp/config.yml).
# bin/script2.pl
use FindBin;
use Cwd qw/realpath/;
use Dancer ':script';
#tell the Dancer where the app lives
my $appdir=realpath( "$FindBin::Bin/..");
Dancer::Config::setting('appdir',$appdir);
Dancer::Config::load();
#getter
print "environment:".config->{environment}."\n"; #development
print "log:".config->{log}."\n"; #value from development environment
By default Dancer loads development environment (typically $webapp/environment/development.yml). If you want to load an environment other than the default, try this:
# bin/script2.pl
use Dancer ':script';
#tell the Dancer where the app lives
Dancer::Config::setting('appdir','/path/to/app/dir');
#which environment to load
config->{environment}='production';
Dancer::Config::load();
#getter
print "log:".config->{log}."\n"; #has value from production environment

Using environment variables in camel.xml to configure endpoints

I have a cxf endpoint configuration in my camel.xml like this:
<cxf:cxfEndpoint id="callbackInbound"
serviceClass="ch.axpo.emis.v1.timeseriesservice.Callback"
wsdlURL="wsdl/timeseries.wsdl" endpointName="tss:CallbackPort"
address="http://somehost.com:9090/CallbackService" serviceName="tss:CallbackService"/>
In one of my routes I call this endpoint like this:
.to("cxf:bean:callbackInbound?dataFormat=PAYLOAD")
So, now instead of having a fix address (http://somehost.com:9090/CallbackService) I want to be able to configure the address for different environments (DEV, TEST, PROD, ...) using system variables. This is because I use JBoss 7 as runtime environment for camel and there is a quite simple way to add system variables with JBoss.
Is there a way to do that? Or is there a better way to configure cxf endpoints in different environments?
Thanks,
Sven
You can use the properties components and define the cxfEndpoint like this
System.setProperty("environment", "junit");
<cxf:cxfEndpoint id="routerEndpoint" address="{{router.address}}"
serviceClass="org.apache.camel.component.cxf.HelloService"/>
You can define the property file just like this
router.address={{{{environment}}.router.address}}
# LOCAL SERVER
junit.router.address=junit
# LOCAL SERVER
local.router.address=local
# TEST
test.router.address=test
# PROD
prod.router.address=prod

Zend 1.10 place websites in virtual subdirectories

I have the following situation:
We have a webapp built with Zend Framework 1.10 that is available under www.domain.com/webapp
On the server filesystem, we really also have the webapp deployed in /srv/www/webapp
Now, for some reasons I can't detail too much, the project manager has requested, now that the app is finished, that each client recieves his own url litteraly.
So we would have:
www.domain.com/webapp/client1
www.domain.com/webapp/client2
Normally, what start after the webapp/ would be the controllers, actions and so forth from zend.
Therefore the question: is there a quick way in apache to create these virtual subdirectories, as in the example, client1, client2 ?
I guess it should be possible with url rewriting ?
Thanks in advance
Rather than creating virtual directories, this can be solved by creating a specific route with Zend_Route. Assuming, controller as User and the action to pass the name would be view, then
$route = new Zend_Controller_Router_Route(
'webapp/:username',
array(
'controller' => 'user',
'action' => 'view',
'username' => 'defaultuser'
)
);

Git - Push to Deploy and Removing Dev Config

So I'm writing a Facebook App using Rails, and hosted on Heroku.
On Heroku, you deploy by pushing your repo to the server.
When I do this, I'd like it to automatically change a few dev settings (facebook secret, for example) to production settings.
What's the best way to do this? Git hook?
There are a couple of common practices to handle this situation if you don't want to use Git hooks or other methods to modify the actual code upon deploy.
Environment Based Configuration
If you don't mind having the production values your configuration settings in your repository, you can make them environment based. I sometimes use something like this:
# config/application.yml
default:
facebook:
app_id: app_id_for_dev_and_test
app_secret: app_secret_for_dev_and_test
api_key: api_key_for_dev_and_test
production:
facebook:
app_id: app_id_for_production
app_secret: app_secret_for_production
api_key: api_key_for_production
# config/initializers/app_config.rb
require 'yaml'
yaml_data = YAML::load(ERB.new(IO.read(File.join(Rails.root, 'config', 'application.yml'))).result)
config = yaml_data["default"]
begin
config.merge! yaml_data[Rails.env]
rescue TypeError
# nothing specified for this environment; do nothing
end
APP_CONFIG = HashWithIndifferentAccess.new(config)
Now you can access the data via, for instance, APP_CONFIG[:facebook][:app_id], and the value will automatically be different based on which environment the application was booted in.
Environment Variables Based Configuration
Another option is to specify production data via environment variables. Heroku allows you to do this via config vars.
Set up your code to use a value based on the environment (maybe with optional defaults):
facebook_app_id = ENV['FB_APP_ID'] || 'some default value'
Create the production config var on Heroku by typing on a console:
heroku config:add FB_APP_ID=the_fb_app_id_to_use
Now ENV['FB_APP_ID'] is the_fb_app_id_to_use on production (Heroku), and 'some default value' in development and test.
The Heroku documentation linked above has some more detailed information on this strategy.
You can explore the idea of a content filter, based on a 'smudge' script executed automatically on checkout.
You would declare:
some (versioned) template files
some value files
a (versioned) smudge script able to recognize its execution environment and generate the necessary (non-versioned) final files from the value files or (for more sensitive information) from other sources external to the Git repo.

Symfony2 DBAL & ORM Setup

So I have been fiddling around with Symfony2 all morning and read the main documentation or at least half of it. I am stuck at anything related to Database.
My simple question is: do we make the database structure before hand or not?
Documentation says make the Entity class and then generate the database table using database:create on CLI. I followed and made a blog entity class with orm annotations.
ran the command:
php app/console doctrine:database:create
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Applications/MAMP/htdocs/flairbagSy2/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php on line 36
Could not create database for connection named blog
SQLSTATE[HY000] [2002] No such file or directory
I think this has something to do with the location of the mysql socket file but I don't know how to change the path of the socket file in Symfony2's configuration.
If anyone could just point out where do I change the path of socket file.
I once had a similar problem with CakePHP and the simple fix was to add a port key to the db connection array:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'cake',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock',
);
How do I do that in Symfony2.
The doctrine:database:create command will not populate the structure of the tables but rather create the database on your database manager like MySQL. To populate the database structure, use the doctrine:schema:create command for the initial creation of the structure and the doctrine:schema:update --force command when you want to update the structure after you made some modifications to the annotations.
As you have mentionned, you need to configure some parameters in Symfony2 for Doctrine to work correctly. Those parameters need to be set in the config.yml configuration file that you can find in the app/config folder of the standard Symfony2 distribution. Here's a sample of the configuration you need to add to the config.yml file:
# Doctrine Services Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: 3396
dbname: dbname_dev
user: dbname_dev
password: yourpassword
logging: "%kernel.debug%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
mappings:
AcmeBundle: ~
The first part of the configuration, the dbal part, is used configure your database connection information. I would suggest you to first test the connection with the root account of your database and then when everything is working well, change the settings to use a dedicated user. This way, you are sure that you don't have some privileges problems or other kind of problems that could be related to the dedicated user.
The second part, the orm part, is use to configure the mapping of your entities to the database. This part is required if you want Symfony2 to populate the database structure automatically. Replace AcmeBundle with the name of your bundle in the sample above to enable the orm functionalities in your project.
On a side note, the Symfony2 team is working on simplifying the configuration and I think there is now an easier way to configure the Doctrine parameters. The sample I propose you is the one I am using right now with the Beta1 of Symfony2.
And finally to answer your question, I think the best is to let Symfony2 deals with the creation and update the database structure. One argument in favor of this would be that you don't need to maintain an SQL file dealing with the database structure. This information is implicitely held by the annotations in your entity so you just have to change you annotations and call the doctrine:schema:update --force command to update the structure. With this in mind, you should not create the structure before hand but let Symfony2 create it for you.
Here's the workflow to test that everything is working:
Update your config.yml (in app/config) to include Doctrine configuration parameters
Open a prompt and go to the root folder of the Symfony distribution (where you see the app, src, vendor and web folders)
Run php app/console doctrine:database:create to create the database in MySQL
Run php app/console doctrine:schema:create to create the database structure
Check in MySQL that the database and the structure have been created correctly
Hope this help.
Regards,
Matt
The original question asked about how to define a non-standard socket in the Symfony2 configuration.
The way I do it is to add a new config setting in app/config/config.yml that pulls in the value from the project parameters file (app/config/parameters.ini).
In app/config/parameters.ini, add a database_socket value:
;[parameters.ini]
database_socket = "/var/mysql/mysql.sock"
and then in app/config/config.yml, pull in the parameter value:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
unix_socket: %database_socket%
charset: UTF8
That will pass the custom socket path into doctrine's connection.
If using MAMP You can create a symbolic link in your /var/mysql/ folder to /Applications/MAMP/tmp/mysql/mysql.sock file
First check to see if your /var/mysql folder exists if it doesnt make it using:
mkdir /var/mysql
Go to the above folder then write the following to make your symbolic link
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
you might also have timezone issues (not related to the above problem but if you're a mamp user you will probably have this one as well) which you can fix by putting the following in your /private/etc/php.ini file (note: this is not your php.ini file found in your MAMP folder)
date.timezone = "America/Montreal"
Note: you might need to change your actual timezone which may not be the same as mine.
References:
http://www.iamseree.com/application-development/doctrine-error-on-mac-osx-it-is-not-safe-to-rely-on-the-systems-timezone-settings-you-are-required-to-use-the-date-timezone-setting-or-the-date_default_timezone_set-function
http://mikecroteau.wordpress.com/2011/08/07/symfony2-could-not-create-database-for-connection-named-sqlstatehy000-2002-no-such-file-or-directory/