0

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for the tip! I needed to open <?php tag without closing it and place function xx() { } and worked! Cheers!
Maybe a small hint, i always use if(!function_exists('myFunc')) to make sure its unique :)
@DonnyvanV Brilliant! Cheers guys!
Why do people keep adding functions to the global namespace!! -- always assume conflicts --

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.