Skip to content

Commit

Permalink
Improve the documentation and check in DB_CONNECTON in laravelPostSta…
Browse files Browse the repository at this point in the history
…rtAction additionally to DB_HOST
  • Loading branch information
NBZ4live committed May 19, 2020
1 parent f5f84d6 commit 4a878b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
47 changes: 47 additions & 0 deletions docs/users/cli-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ Note that Magento 2 is a huge codebase and using `nfs_mount_enabled: true` is re
### Laravel Quickstart

Get started with Laravel projects on ddev either using a new or existing composer project or by cloning a git repository.
The Laravel project type can be used for Lumen same as for Laravel.

#### Laravel Composer Setup Example

Expand All @@ -326,8 +327,54 @@ git clone https://github.com/example/example-site
cd example-site
ddev config --project-type=laravel
ddev composer install
ddev exec "cat .env.example | sed -E 's/DB_(HOST|DATABASE|USERNAME|PASSWORD)=(.*)/DB_\1=db/g' > .env"
ddev exec "php artisan key:generate"
ddev launch
```

#### Laravel Database connection

In the examples above we used a one liner to copy `.env.example` as `env`and set the `DB_HOST`, `DB_DATABASE`, `DB_USERNAME` and `DB_PASSWORD` environment variables to the value of `db`.
These values are DDEV's default settings for the Database connection.

Instead of setting each connection variable we can add a ddev to the `connections` array in `config/databases.php` like this:

```
<?php
return [
...
'connections' => [
...
'ddev' => [
'driver' => 'mysql',
'host' => 'db',
'port' => 3306,
'database' => 'db',
'username' => 'db',
'password' => 'db',
'unix_socket' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
...
];
```

This way we only need to change the value of `DB_CONNECTION` to `ddev` in the `.env` to work with the `db` service.
This is very handy if you have a local database installed and you want to switch between the connections faster by changing only one variable in `.env`

### Database Imports

Import a database with just one command; We offer support for several file formats, including: **.sql, sql.gz, mysql, mysql.gz, tar, tar.gz, and zip**.
Expand Down
6 changes: 4 additions & 2 deletions pkg/ddevapp/laravel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ func envSettingsWarning(status int) {
util.Warning(message)
util.Warning("You can do it with this one-liner:")
util.Warning("ddev exec \"cat %v | sed -E 's/DB_(HOST|DATABASE|USERNAME|PASSWORD)=(.*)/DB_\\1=db/g' > .env\"", srcFile)
util.Warning("Read more on https://ddev.readthedocs.io/en/stable/users/cli-usage/#laravel-quickstart")
}

func laravelPostStartAction(app *DdevApp) error {
if fileutil.FileExists(filepath.Join(app.AppRoot, ".env")) {
isConfigured, err := fileutil.FgrepStringInFile(app.SiteSettingsPath, `DB_HOST=db`)
if err == nil && !isConfigured {
isConfiguredDbHost, err := fileutil.FgrepStringInFile(app.SiteSettingsPath, `DB_HOST=db`)
isConfiguredDbConnection, _ := fileutil.FgrepStringInFile(app.SiteSettingsPath, `DB_CONNECTION=ddev`)
if err == nil && !isConfiguredDbHost && !isConfiguredDbConnection {
envSettingsWarning(WarnTypeNotConfigured)
}
} else {
Expand Down

0 comments on commit 4a878b8

Please sign in to comment.