Symfony / PHP - Best way to store single value

Active3 hr before
Viewed126 times

3 Answers


Question : What is the best way to store a single value that gonna change over some actions.,Assuming that I have an application running on Symfony 3.2 / Doctrine.,Can react to specific property or relation changes to specific value,I have also have thought about storing that value on a text file, but im saying that there might be another (better and more optimized) solution to this scenario.

Here is how you create a connection to a Database with the doctrine Connection class:

/** @var DoctrineBundleDoctrineBundleConnectionFactory $connectionFactory */
$connectionFactory = $this - > getContainer() - > get('doctrine.dbal.connection_factory');
$connection = $connectionFactory - > createConnection(
   array('pdo' => new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password))

Now you can use $connection as a simple PDO object:

$connection - > executeQuery('SELECT * FROM your_table');

You could add this code as a service to make it accessible everywhere.
If you want to connect to a different database for a different domain you can use this code to identify the domain:

$this - > getRequest() - > getHost();

To access the domain in an action do this:

function yourAction(Request $request, /* ... */ ) {
   // the Controller extends the Container. So need to get it here:
   $connectionFactory = $this - > get('doctrine.dbal.connection_factory');

   // also access the domain like this:
   $domain = $request - > getHost();
load more v

The values of these options change from one machine to another (e.g. from your development machine to the production server) but they don't modify the application behavior.,If you need to reuse some feature in your projects, create a bundle for it (in a private repository, to not make it publicly available). For the rest of your application code, use PHP namespaces to organize code instead of bundles., Don't Create any Bundle to Organize your Application Logic ,These are the options used to modify the application behavior, such as the sender of email notifications, or the enabled feature toggles. Their value doesn't change per machine, so don't define them as environment variables.

$ symfony new my_project_name
load more v

Laravel configuration looks very different from that of Symfony. The only one thing they have in common is that they both use files that are not stored under version control (.env in the Laravel case) and a template for generating this file (.env.example). This file has a list of keys and values, like the following example:,The Laravel installation process is almost the same and as simple as that for Symfony; the only difference is that you install Laravel’s installer through Composer:,Like the Symfony YAML file, this one for Laravel is also human readable and looks clean. You can additionally create .env.testing file that will be used when running PHPUnit tests.,However, you may feel more comfortable with the default PHP configuration syntax Laravel is using and you don’t have to learn YAML syntax.

Symfony installation is as simple as the following:

# Downloading Symfony installer
sudo curl - LsS https: // -o /usr/local/bin/symfony
   # Granting permissions to execute installer
sudo chmod a + x / usr / local / bin / symfony
# Creating new Symfony project
symfony new symfony_project
# Launching built - in server
cd symfony_project / && php bin / console server: start
load more v

Other "symfony-undefined" queries related to "Symfony / PHP - Best way to store single value"