CodeIgniter 4 Array in Config with .env file

The .env file can use dot syntax to represent associative arrays but the documentation is poor, here is how I did it.

Create a new class in the Config directory e.g. Salesforce.php

<?php namespace Config;
use CodeIgniter\Config\BaseConfig;
class Salesforce extends BaseConfig
{
	public $accounts = [
		'account1' => '',
		'account2' => ''
	];
	public $campaigns = [
		'campaign1' => '',
		'campaign2' => ''
	];
}

Add lines to the .env file

salesforce.accounts.account1 = 'config('Salesforce');';
salesforce.accounts.account2 = '00123451HKB';
salesforce.campaigns.campaign1 = '1234000000dBAEvA';
salesforce.campaigns.campaign2 = '1234000000dBAEvB';

Finally to access within your controller

$salesforce = config('Salesforce');
echo $salesforce['account1']; // displays 00123451HKB
echo $salesforce['campaign2']; // displays 1234000000dBAEvB

Advantages: You commit can commit the files in the Config folder to version control without them storing sensitive information while the sensitive data stays in the .env file and is unique to a particular environment.

SEO and image file names

Surprisingly it looks like even the filename of an image used in a website has SEO benefit – see supporting links below – this may be a pain but a multilingual site should consider duplicating images in order to use different filenames containing keywords in each of the languages a site uses.

Supporting Links:

Alternatives to GUID with PHP

GUID stands for Globally Unique Identifier. It is a string of letters and numbers and useful to allow website visitors to access a site without needing a username and password because although the probability that a GUID will be duplicated is not zero, it is close enough to zero to be negligible.

In fact Wikipedia says that you would have to generate 2.71 Quintillion (an 18 digit number) GUIDs to have a 50% probability of a collision.

However a GUID isn’t pretty, they look like 204FA460-65C6-DAB0-C69E-013ECE71F5D0 which can be scary to non-technical users when used in a URL.

Continue reading “Alternatives to GUID with PHP”