senty
senty

Reputation: 12847

Where to write Custom Helpers in Laravel?

I want to have a little string converter (translator) helper method. For example, I want to pass my string from the controller and get the result, so I don't want to repeatedly write the same helper function into the controller.

So that I can maybe use it like:

$oldStr = '12 Mayıs 2014';  // which means 12 May in English
$englishStr = custom_date_translator($oldStr);

and it does it's thing on the helper method.

Is the Service Providers for this purpose? It'd be good to learn the right way to do it.

Upvotes: 0

Views: 172

Answers (1)

Drown
Drown

Reputation: 5982

You can create a custom helpers.php file and setup Composer so that it's autoloaded.

For example, if you create the helpers.php file in the app/ directory, you could edit your composer.json file to something like this :

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

Make sure to run composer dump-autoload in your console to update the autoloader.

Upvotes: 5

Related Questions