Skip to content

Latest commit

 

History

History
45 lines (36 loc) · 1.49 KB

6.Helpers.md

File metadata and controls

45 lines (36 loc) · 1.49 KB

6. Helpers

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.

6.1 Defining

6.1.1 Autoload Using Composer

  1. In composer.json, add this to autoload key.

    "autoload": {
        "files": [
            "app/Helpers/Helper.php"
        ]
    },
  2. In app/Helpers, create a file called Helper.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

  3. 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')
    {
        // ....
    }