does silex make new database connection for every request? - php

I'm new to Silex/PHP. I have the following snippet in my index.php.
$app->register(new \Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => $dbConfig
));
dbConfig - is configurable (pdo_sqlite or pdo_mysql). Silex is run under lighttpd with php-fpm.
I have following questions:
Does Silex create "new" database connection for every request? If so, how does one optimize for performance so old/existing connection is used?
If new connection is not made, then how can one change the existing connection from one to other for the particular "app" instance when the database configuration changes? In this case, please assume that "app" is the one that process the configuration change and gets to know about it.
Thanks in advance.

Related

Queued route() urls has wrong domain in URL

I have a Laravel 5.1 application that has many clients with their own unique subdomains and databases.
On app loading, my middlewares resolves the client and sets the app.url (config/app.php) accordingly. It all works great, in the browser. All urls generated by route() has the correct subdomain for current client.
But, stuff queued (Redis in my case) will always defaults to a domain URL of "localhost".
So if I send a welcome email where the text template contains route('account') it will from the queue generate a "http://localhost/account" URL. This is of course not correct.
I've found the line that probably does this, it's in the Illuminate\Foundation\Bootstrap\SetRequestForConsole class:
$url = $app->make('config')->get('app.url', 'http://localhost'); <---
As far as I can see, I can't really "hook" into anything before that.
Info: For each Queued command (closure) I have, I always send with it who the client is that's queing. That way I set the client before the queued command is fired. It loads the right database connection. But changing the default route() server name appears rather difficult!
I've experimented with extending the UrlGenerator class, but it appears that this is completely ignored for queued commands and only works on HTTP requests.
I've also tried adding this before the queues command is fired:
app('url')->forceRootUrl($client->getClientUrl());
It did not work. (but does work with HTTP requests)
Anyone here have an idea for how to set my own default domain for route() in CLI mode?
In your environment config (.env) file, add an entry:
APP_URL='http://www.example.com'
Then in your config/app.php file change the application url from localhost:
----------------
Application URL
----------------
...
...
//'url' => 'http://localhost',
'url' => env('APP_URL'),
...
These two changes worked for me when I used the database queue driver in Laravel 5.1.
I case you have sub domain and your want to make route url with sub-domain so in this case there might be problem.
I have used this in my app. config/app.php file
...
...
//'url' => 'http://localhost',
'url' => url('/'),
...

Laravel 4 - environment configuration

I am working on a Laravel 4 project and I need to be able to switch between multiple configurations. So as far as I know Laravel enables me to configure envs based on URL like this in the start.php
$env = $app->detectEnvironment(array(
'local' => array('localhost'),
'stage' => array('project.stage.com'),
'prod' => array('project.production.com'),
));
And each of this configs consists of separate database connections and other configuration files. What I want is on my local environment to be able to switch between local,stage and prod, so for example if I want to connect to the prod database from my local project to test something. As far as I can understand if I want to do this I need to manualy switch the database connection strings in the local configuration. Is there any other way for switching between configurations on local level ? Hope my question was clear.
You can pass a closure to the function to determine set the environment more dynamically. You can either replicate the logic laravel uses and only use the closure in combination with gethostname() or just comment the part out and add this for testing:
$app->detectEnvironment(function(){
return 'stage';
});

Codeigniter: Share database config between applications

I have two applications inside one Codeigniter instance (Codeigniter 3.0). I need to share database config between applications. In each application I have config:
include_once 'application/shared/config/database.php';
But I have error on application start:
No database connection settings were found in the database config
file.
I decide to check variable
include_once 'application/shared/config/database.php';
var_dump(isset($db));
And what I see?
boolean true
boolean false
I'm trying to find difference between DB initializations, but not founded yet.
include instead of include_once is solution, but I think it is not correct.
What is the best solution?

mysqldump-php not working with shared hosting companies database. No command line access or SUPER privilege(s)

Does anyone know if mysqldump-php can work with shared hosting? I can get it to work on my local computer (I can get mysqldump to work locally also) but I need a method to backup a database that's hosted with a popular webhosting company. The only method that they offer to their customers is to sign onto phpMyAdmin and download your .sql manually. daily. yourself. no automation allowed. I'm a newbie and I'm going nuts trying to find a solution.
mysqldump-php called for
namespace Ifsnop\Mysqldump;
use Exception;
use PDO;
use PDOException;
It didn't mention needing command line access or SUPER privilege(s).
Am I using incorrect settings?
Here's the link to the code that I'm using on Github.
https://github.com/ifsnop/mysqldump-php#dump-settings
Any help would be so appreciated!
I've had a look in the code of ifsnop, and it uses SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ before starting the transaction. Global settings are only allowed with the SUPER-privilege (which you don't have on a shared environment, with a good reason ;) ).
You can either:
If you have only MyISAM-tables, there is no need for a transaction, so it can be turned off in the config.
$dumpConfig = array(
'single-transaction' => false
);
$md = new Mysqldump($db, $user, $pass, $host, 'mysql', $dumpConfig);
or; Change that word GLOBAL into SESSION (thus modifying the downloaded code).

Doctrine error: "Failed opening required '/tmp/__CG__Source.php' "

I am trying to migrate my PHP application to an Ubuntu server, but without succes. Any help would be appreciated.
First I installed Doctrine successfully into /jorrit/myapp, following the first part of Doctrine's Getting Started manual (till "Generating the Database Schema"). Secondly I placed my PHP scripts (which use Doctrine) in folder /jorrit/myapp.
When I try to run my PHP script in the CLI, I get this error messages:
PHP Warning: require(/tmp/__CG__Source.php): failed to open stream: No such file or directory in /jorrit/myapp/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php on line 200
PHP Fatal error: require(): Failed opening required '/tmp/__CG__Source.php' (include_path='.:/usr/share/php:/usr/share/pear') in /jorrit/myapp/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php on line 200
Bootstrap.php looks like this:
<?php
// bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once "vendor/autoload.php";
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = false;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => 'xx',
'user' => 'xx',
'password' => 'xx',
'dbname' => 'xx',
'profiler' => 'false'
);
// obtaining the entity manager
$entityManager = EntityManager::create($dbParams, $config);
?>
The first lines of my PHP script:
<?php
require_once "bootstrap.php";
require_once 'classes.php';
$connection = $entityManager->getConnection();
The application works fine in my development environment (Windows). The /tmp folder exists and is accessible. The database is migrated succesfully and exists. I did not change anything in the vendor folder.
Any ideas? Thanks in advance for your help.
TL;DR You'll just need to generate your proxy classes manually
vendor/bin/doctrine orm:generate-proxies
Doctrine uses Proxies to connect the to database. Proxies are generated from the the Entity classes.
In development mode, it generates a Proxies on every request because you could make changes to Entity classes.
In production mode, it does not generate Proxies every time. For performance reason, it assumes the Proxies exist and include them directly.
There are a few mode for Proxies generation:
ALWAYS - It alwayes generates Proxies, this is the default setting for development mode
NEVER - It never generates Proxies, this is the default setting for production mode
ON_DEMAND - It only generates the Proxies if the Proxy files do not exist. The drawback of this option is that it has to call file_exists() every time which could potentially cause a performance issue.
Now the command
vendor/bin/doctrine orm:generate-proxies
generates Proxy classes to /tmp. I would say this might still cause trouble because other applications
on your server might delete these files unexpectedlly. One option is you can change your /tmp directory access permission to 1777
sudo chmod 1777 /tmp
The stricky bit '1' in front of 777 means that, although everyone can read/write to the /tmp directory, but you can only operate on your own files. i.e. You can't remove files created by other users.
For further reading, please have a look at
http://docs.doctrine-project.org/en/latest/reference/advanced-configuration.html#auto-generating-proxy-classes-optional
You can also set the Proxies directory to somewhere else so no other applications can modify them. http://docs.doctrine-project.org/en/latest/reference/advanced-configuration.html#autoloading-proxies
In code after $config line you could try
$config->setAutoGenerateProxyClasses(true);
But the CLI version is much better, because it avoids on refresh regen as in code might not avoid.
To change cache dir you could try:
$cacheDir = dirname(__FILE__).'/cache';
if (!is_dir($cacheDir)) {
mkdir($cacheDir);
}
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, $cacheDir);
Looks like a permission problem, first should chek on permissions for the entire application folder.
Also try to hard-cleanup cache by deleting app/cache/* files, and try again.
Good luck!

Categories