Answer the question
In order to leave comments, you need to log in
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
In composer.json something like this
"autoload": {
....
"files": [
"app/helpers.php"
]
},
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 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 questionAsk a Question
731 491 924 answers to any question