Jump to content

Namespace problems in ProcessWire 3.0.8


steveooo
 Share

Recommended Posts

Hello,

PW 3 uses namespaces and Composer.

I wanted to include https://github.com/vlucas/phpdotenv for having a nice .env file with all my DB settings.

On 2.7 I simply included everything in the top of the index.php:

// Custom
require __DIR__ . '/vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

Now, composer is there so I used "composer require vlucas/phpdotenv" and wanted to use it. The current index.php includes the autoloader via:

$composerAutoloader = $rootPath . '/vendor/autoload.php'; // composer autoloader
if(file_exists($composerAutoloader)) require_once($composerAutoloader);

So after that, I tried to add:

// Custom
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

but the error is:

Fatal error: Class 'ProcessWire\Dotenv\Dotenv' not found in /var/www/index.php on line 36

It's because on top of the index, there is 

<?php namespace ProcessWire;

So Dotenv is interpreted like a child of ProcessWire. How can I change this?

If interesting, here is my composer.json:

{
  "name": "processwire/processwire",
  "type": "library",
  "description": "ProcessWire CMS/CMF",
  "keywords": [ "cms","cmf", "content management system" ],
  "homepage": "https://processwire.com",
  "authors": [
    {
      "name": "Ryan Cramer",
      "email": "ryan@processwire.com",
      "homepage": "https://processwire.com",
      "role": "Developer"
    }
  ],
  "require": {
    "php": ">=5.3.8",
    "ext-gd": "*",
    "vlucas/phpdotenv": "^2.2"
  },
  "autoload": {
    "files": [ "wire/core/ProcessWire.php" ]
  }
}

Thanks for your answers!

Link to comment
Share on other sites

@LostKobrakai:

Well, Dotenv is used so I can write my DB credentials to .env which I can exclude from Git. In the site/config.php, I must then change everything to e.g. getenv('DB_PASSWORD')...

So should I include my Dotenv in site/config.php or where would be a good place for that? 

Link to comment
Share on other sites

Exactly. config.php is just a php files as well. You can do whatever you need there as long as you're at sometime setting the correct properties to the $config variable. But I'd limit it to things really needed at that stage. Anything else should rather go into init or ready.php.

  • Like 1
Link to comment
Share on other sites

@steevooo

If it is just about storing local configurations you can also use config-dev.php in your sites folder and put this file in the .gitignore

https://processwire.com/api/variables/config/

Maintaining a separate development configuration file

You may optionally maintain a separate development configuration file at /site/config-dev.php. When present, ProcessWire will use this file rather than /site/config.php. This may be useful on your development and/or staging server and allows you to maintain the same /site/config.php file between staging and production server. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...