I know this is an old question, and the latest answer is still pretty old, so I thought I would put my 2 cents in for the 2023 fresh install of Yii2.
First I create a new folder called globals
. Inside that, I created a php file called functions.php
.
I place all of my global functions in there, I also did the same for classes.php
Here is an example for the functions
<?php
use yii\helpers\Url;
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
use yii\helpers\ArrayHelper;
// cents: 0=never, 1=if needed, 2=always
function formatMoney($number, $cents = 2) {
if (is_numeric($number)) {
if (!$number) {
$money = ($cents == 2 ? '0.00' : '0');
} else {
if (floor($number) == $number) {
$money = number_format($number, ($cents == 2 ? 2 : 0));
} else {
$money = number_format(round($number, 2), ($cents == 0 ? 0 : 2));
}
}
return '$'.$money;
}
}
function formatPhoneNumber($phoneNumber) {
$phoneNumber = preg_replace('/[^0-9]/','',$phoneNumber);
if(strlen($phoneNumber) > 10) {
$countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);
$areaCode = substr($phoneNumber, -10, 3);
$nextThree = substr($phoneNumber, -7, 3);
$lastFour = substr($phoneNumber, -4, 4);
$phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;
}
else if(strlen($phoneNumber) == 10) {
$areaCode = substr($phoneNumber, 0, 3);
$nextThree = substr($phoneNumber, 3, 3);
$lastFour = substr($phoneNumber, 6, 4);
$phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;
}
else if(strlen($phoneNumber) == 7) {
$nextThree = substr($phoneNumber, 0, 3);
$lastFour = substr($phoneNumber, 3, 4);
$phoneNumber = $nextThree.'-'.$lastFour;
}
return $phoneNumber;
}
Then in the root, I opened the composer.json
file. After the
"scripts": {
...
}
I added:
"autoload": {
"files": [
"\\globals\\functions.php"
]
},
Then in the yii
file, I added require_once(__DIR__ . '/globals/functions.php');
#!/usr/bin/env php
<?php
/**
* Yii console bootstrap file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/config/console.php';
$application = new yii\console\Application($config);
require_once(__DIR__ . '/globals/functions.php');
$exitCode = $application->run();
exit($exitCode);
Once all of these changes have been made, I then ran composer update
and the ./yii serve 0.0.0.0
Now you can use your functions as if they are in the file you are developing.
<?php foreach ($bills as $bill): ?>
<tr>
<th scope="row"><?= $bill['company_name'] ?></th>
<td><a href="<?= $bill['company_website'] ?>" target="_blank"><?= $bill['company_credit_name'] ?></a></td>
<td class="text-center"><?= $bill['type_name'] == 'credit' ? Icon::show('credit-card', ['class'=>'fa-2x']) : Icon::show('file-invoice-dollar', ['class'=>'fa-2x']) ?></td>
<td class="text-center"><?= $bill['bill_autopay'] == '1' ? Icon::show('toggle-on', ['class'=>'fa-2x, toggle-on']) : Icon::show('toggle-on', ['class'=>'fa-2x, toggle-off']) ?></td>
<td><?= formatMoney($bill['bill_credit']) ?></td>
<td><?= formatMoney($bill['bill_balance']) ?></td>
<td><?= formatMoney($bill['bill_payment']) ?></td>
<td><?= $bill['bill_dueday'] ?></td>
<td><?= $bill['bill_rate'] ?>%</td>
<td><?= $bill['bill_cpi'] == '1' ? true : false ?></td>
<td><?= formatMoney($bill['bill_perid']) ?></td>
</tr>
<?php endforeach; ?>
This is my take on how to do this.