Helpers are functions that are used for handling a common operation throughout an application. For eg, sorting of an array alphabetically, flattening of an array, creating a string with first letter capital of each word and more. These functions come in really handy through out the application and can help remove redundancy in the code.
Note: You must avoid creating helper functions for methods like transforming request inputs since they belong to a service only. If however, the transformation of inputs is common for all services, consider creating a common or base service class and extend it to the services requiring this method.
-
In
composer.json
, add this to autoload key."autoload": { "files": [ "app/Helpers/Helper.php" ] },
-
In
app/Helpers
, create a file calledHelper.php
and add this<?php require 'FileName.php';
Try creating files based on the entity the functions might be based on. For eg. Storage.php for file storage related functions
-
Now in any of the file, create php functions using the snake_case naming convention.
/** * Upload Files To A Directory * * @param array $files * @param int $id * @param string $path * @param string $model * * @return array */ function upload_files(array $files, $id, $path = '', $model = 'user') { // .... }