Skip to main content
+234 803-795-2621
Laravel Custom Helper Functions
Mon, 10/02/2017 - 11:46

Creating Custom Laravel Helpers

Helper functions are just to make writing Laravel views easier, but I feel people will start writing tons of poorly organized functional PHP again.

Never mind, let's learn how to create one.

Create a helpers.php file
First create the file app/helpers.php.


You may wish to try this as your first helper function:

<?php
// A simple description of my function could come here
function returnString()
{
    return 'Hello world custom helper';
}
?>


Load it up with composer

Change your composer.json file and dump the autoloader

...what you might see :

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

...what you should do: add a comma seperator ","  before this:


    "files": [
        "app/helpers.php" 
    ]

 what you should get :

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/helpers.php" 
    ]
},

After adding that to your composer.json file, run the following command:

composer dump-autoload

You use this command after changing composer.json.

Category

Are you ready to take your business global?

Request a quote
click here.