1

I inherited a Craft site which uses phpdotenv (which I'm unfamiliar with) and which I now have to make multi-lingual (https://craftcms.com/docs/localization-guide) with an added es and fr locale on top of the english language site. The question is regarding the setup on MAMP which seems to never find the .env file. On my production server in /es/index.php this works:

// Composer autoload
require_once('../../vendor/autoload.php');

// DotEnv
try {
    $dotenv = new Dotenv\Dotenv('../../');
    $dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
} catch (Exception $e) {
    exit('Could not find a .env file.');
}

// Path to your craft/ folder
$craftPath = '../../craft';

// Tell Craft to serve the spanish content
define('CRAFT_LOCALE', 'es');

However, the same code on my local MAMP install will not find the .env file no matter what path I enter into the new dotenv statement.... Anyone have any idea or suggestion? Truly stumped here.

2 Answers 2

3

Some troubleshooting steps:

  • Make sure a vendor/ folder exists two levels up from es/. If not, you need to run composer install in your terminal from the directory that contains a composer.json file. (If you don’t have Composer installed, see its installation instructions.)

  • Make sure a .env file exists two levels up from es/. It probably won’t, as you’re not supposed to include your .env file in the Git repo. You can copy it from production, or from .env.example if that exists. Then fill it in with working credentials for your local environment.

Matt Stauffer wrote a great article explaining how to get phpdotenv running with Craft 2: https://mattstauffer.com/blog/environment-specific-configuration-for-craftcms-using-phpdotenv/ Probably a good idea to read through that just to get an idea of how it’s all supposed to work (and what the advantages are).

3
  • Thanks for the link to the article Brandon. Both the /vendor folder and the .env file are there though and in the correct spot. Jan 21, 2018 at 0:25
  • @erwinheiser ok try removing the try and catch lines, so the actual exception will be allowed to surfaces what does it say? Jan 21, 2018 at 1:08
  • If I do that it's just a blank screen. Jan 21, 2018 at 13:56
3

My current setup for multilingual sites:

  • as Brandon said, make sure there is a vendor/ folder at project root level (there should be one in this case otherwise the site wouldn't run).
  • extract the phpdotenv part of the code from your base index.php and put it into an env.php that you create at the same level as your main index.php file.
  • require this new file at the top of your main index.php using require_once 'env.php';
  • check that this more modular install is running fine (it should since you have only abstracted the phpdotenv part)
  • in your fr/ and es/ locales folders, just copy your main index.php, change the relative paths (the require once and the craft folder) and define your Craft locale.
  • you should be set

Here is a rough draft of what you should get to

public/env.php

// Composer autoload (assuming '/vendor' is at the root and this is in '/public'
require_once('../vendor/autoload.php');

// DotEnv
try {
  $dotenv = new Dotenv\Dotenv(dirname(__DIR__));
  $dotenv->load();
  $dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
} catch (Exception $e) {
  exit('Could not find a .env file.');
}

public/index.php

<?php
// require 'env.php' only once
require_once 'env.php';

// Path to your craft/ folder
$craftPath = '../craft';

// Tell Craft to serve the French content
define('CRAFT_LOCALE', 'en');

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
    if (function_exists('http_response_code'))
    {
        http_response_code(503);
    }

    exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}

require_once $path;

public/fr/index.php

<?php
// require 'env.php' only once
require_once '../env.php';

// Path to your craft/ folder
$craftPath = '../../craft';

// Tell Craft to serve the French content
define('CRAFT_LOCALE', 'fr');

// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';

if (!is_file($path))
{
    if (function_exists('http_response_code'))
    {
        http_response_code(503);
    }

    exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}

require_once $path;

phpdotenv and composer are an integral part of Craft3 as well as of all modern PHP apps so it's worth getting familiar with. Basically just dependency management for PHP (Composer) and putting all your sensitive info in a file not committed to repo (phpdotenv).

Hope it helps

3
  • Cheers mate, will test it out today. Just not a fan of adding a layer of complexity on top of things that work fine out of the box. Jan 21, 2018 at 13:48
  • Layer of security, not complexity. Comoser is basically dependency management for PHP and phpdotenv behaves like an include for environment variables and is also a PHP standard. Both are standard in Craft3 github.com/craftcms/docs/blob/master/en/installation.md Jan 21, 2018 at 13:56
  • OK, old dog new tricks I get it. Couldn't get this to work either though but Quentin just pushed some new code that does the trick, apparently the paths have to be juuuust right or it simply fails. Beer's on me next time I'm in Brussels! Jan 21, 2018 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.