Search code examples
phplaravellaravel-5.5laravel-artisanlaravel-helper

Creating a Helper Class for Laravel Artisan Command


I would like to know what's the best approach to create a helper class for my Artisan commands. I want the helper class to contain some static/non-static function to reduce code duplication as well as the size of my commands and to improve code maintainability. I have App/Library folder in which I prefer to place my helper class.


Solution

  • Create a helpers.php file in your app/Library folder (or any location of your choice) and add the following to it:

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

    After, run the following command on your command line:

    composer dump-autoload
    

    You can now add functions by defining them on the helpers.php