F
F
flafy42016-12-08 16:27:02
Laravel
flafy4, 2016-12-08 16:27:02

Where to write custom functions in laravel?

Good afternoon everyone. Where can I write my functions in the laravel framework so that they can then be called from any site file? Suppose I write a function that will form a slug (change the original sentence, and return it as a transliteration), but this is not important. And so, what then I could cause this function it is admissible from any controller?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav, 2016-12-08
@flafy4

In composer.json something like this

"autoload": {
   ....
        "files": [
            "app/helpers.php"
        ]
    },

Next, create a helpers.php file in the app and add functions there

R
roodz, 2016-12-17
@roodz

1. The task of slug transliteration is successfully solved by the standard helper: it
will return
mama-myla-ramu
2. Custom helpers are created as follows:
add a section to composer.json

"autoload": {
        "files": [
            "app/Support/helpers.php"
        ],
in app, create the Support folder and add helpers.php to it
and write our helper functions to it, for example:
if (! function_exists('copyrightYear')) {
    function copyrightYear(int $startYear, string $dash = '–'): string
    {
        return $startYear . ($startYear == ($currentYear=date("Y")) ? '' : $dash . $currentYear);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question