Skip to content

Update README with connection settings via .env #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update README with .env file
Add security on connection settings via .env
  • Loading branch information
dolcy authored Oct 23, 2018
commit 0970f8c4da409bf44b1f624c646bce1dd9daeb87
49 changes: 38 additions & 11 deletions docs/Drivers/PHP/Tutorial/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
# ArangoDB-PHP - Tutorial
## Setting up the connection options

In order to use ArangoDB, you need to specify the connection options. We do so by creating a PHP array $connectionOptions. Put this code into a file named test.php in your current directory:
In order to use ArangoDB, you need to specify the connection options. We do so by creating a PHP array $connectionOptions.

To keep our connection settings secure, we should create a .env file for storing our sensitive data. First, let’s run ‘composer require vlucas/phpdotenv’. Now create a .env file in your root folder with the following variables:

```
OPTION_DATABASE="_system"
OPTION_ENDPOINT="tcp://127.0.0.1:8529"
OPTION_AUTH_TYPE="Basic"
OPTION_AUTH_USER="root"
OPTION_AUTH_PASSWD=
OPTION_CONNECTION="Keep-Alive"
OPTION_TIMEOUT=3
OPTION_RECONNECT=true
OPTION_CREATE=true

OPTION_MEMCACHED_PERSISTENT_ID=
OPTION_MEMCACHED_SERVERS=
OPTION_MEMCACHED_ENDPOINTS_KEY=
OPTION_MEMCACHED_TTL=
```
Finally, put this code into a file named test.php in your current directory:

```php
// use the following line when using Composer
Expand All @@ -10,6 +30,9 @@ In order to use ArangoDB, you need to specify the connection options. We do so b
// use the following line when using git
require __DIR__ . '/arangodb-php/autoload.php';

// use .env variables for security
use Dotenv\Dotenv;

// set up some aliases for less typing later
use ArangoDBClient\Collection as ArangoCollection;
use ArangoDBClient\CollectionHandler as ArangoCollectionHandler;
Expand All @@ -25,26 +48,30 @@ use ArangoDBClient\ServerException as ArangoServerException;
use ArangoDBClient\Statement as ArangoStatement;
use ArangoDBClient\UpdatePolicy as ArangoUpdatePolicy;

// set up some basic connection options
// set and load environment variable dir
$dotenv = new Dotenv(__DIR__);
$dotenv->load();

// set up some basic connection options via .env
$connectionOptions = [
// database name
ArangoConnectionOptions::OPTION_DATABASE => '_system',
ArangoConnectionOptions::OPTION_DATABASE => $_ENV['OPTION_DATABASE'],
// server endpoint to connect to
ArangoConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529',
ArangoConnectionOptions::OPTION_ENDPOINT => $_ENV['OPTION_ENDPOINT'],
// authorization type to use (currently supported: 'Basic')
ArangoConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
ArangoConnectionOptions::OPTION_AUTH_TYPE => $_ENV['OPTION_AUTH_TYPE'],
// user for basic authorization
ArangoConnectionOptions::OPTION_AUTH_USER => 'root',
ArangoConnectionOptions::OPTION_AUTH_USER => $_ENV['OPTION_AUTH_USER'],
// password for basic authorization
ArangoConnectionOptions::OPTION_AUTH_PASSWD => '',
ArangoConnectionOptions::OPTION_AUTH_PASSWD => $_ENV['OPTION_AUTH_PASSWD'],
// connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
ArangoConnectionOptions::OPTION_CONNECTION => 'Keep-Alive',
ArangoConnectionOptions::OPTION_CONNECTION => $_ENV['OPTION_CONNECTION'],
// connect timeout in seconds
ArangoConnectionOptions::OPTION_TIMEOUT => 3,
ArangoConnectionOptions::OPTION_TIMEOUT => $_ENV['OPTION_TIMEOUT'],
// whether or not to reconnect when a keep-alive connection has timed out on server
ArangoConnectionOptions::OPTION_RECONNECT => true,
ArangoConnectionOptions::OPTION_RECONNECT => $_ENV['OPTION_RECONNECT'],
// optionally create new collections when inserting documents
ArangoConnectionOptions::OPTION_CREATE => true,
ArangoConnectionOptions::OPTION_CREATE => $_ENV['OPTION_CREATE'],
// optionally create new collections when inserting documents
ArangoConnectionOptions::OPTION_UPDATE_POLICY => ArangoUpdatePolicy::LAST,
];
Expand Down