简体   繁体   中英

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.

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

Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient. Laravel documentation on Helper functions

There are lots of resources online to help you with this, I recommend this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM