-
app/Utilities/helpers.php
Open in GitHub// We just create a list of functions that don't belong to any class // But, since they are global functions, it's important to check if their names don't exist somewhere else // Notice: you can create this file in any folder you want. In this case, they created it in app/Utilities if (!function_exists('user')) { function user() { if (request()->isApi()) { $user = app('Dingo\Api\Auth\Auth')->user(); } else { $user = auth()->user(); } return $user; } } if (!function_exists('company_date_format')) { function company_date_format() { $date_time = new class() { use DateTime; }; return $date_time->getCompanyDateFormat(); } } // ... other functions
-
composer.json
Open in GitHub// We autoload that helper file in our composer.json { "name": "akaunting/akaunting", "description": "Free accounting software, based on Laravel framework.", // ... other values "autoload": { "files": [ "app/Utilities/helpers.php" ] }, // ... other values }
-
app/Http/Controllers/Auth/Login.php
Open in GitHub// And we can use functions like user() in any class, like Controller // In fact, user() function is used 11 times over whole project class Login extends Controller { public function store(Request $request) { // Get user object $user = user(); // Check if user is enabled if (!$user->enabled) { $this->logout(); return response()->json([ 'status' => null, 'success' => false, 'error' => true, 'message' => trans('auth.disabled'), 'data' => null, 'redirect' => null, ]); } // ... other code of this method } }