laravel creating a helper
-
20-12-2019 - |
Vra
I have created file helpers.php
(app/helpers.php)
function test($var)
{
return $var;
}
I have changed app/start/global.php into
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/helpers.php',
));
Also composer.json into
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/helpers.php"
]
},
But when am I trying to call this function inside controller
$data = Test::all();
return test($data);
It throws me an error
Call to undefined function test()
What's wrong?
Oplossing
add -
require app_path().'/helpers.php';
at the end of - app/start/global.php
you dont have to add it to composer.json and set -
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
hope this will work.
Ander wenke
@sgt's approach works fine, but if you still want to use composer, use the files
autoloader.
composer.json
"autoload": {
...
"files": [
"path/to/my/file.php"
]
...
}
Don't forget to composer dump-autoload
after doing a change to the autoload
directive.
Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow