Hello Guys I tried To Made A Helper Function Like asset Using Laravel 9.21.6
This Is My Code
Helper.php
<?php
function show_name() {
return 'Ahmed Emmam';
}
routes.php
Route::get('test-helper' , function () {
show_name();
});
composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"files" : [
"app/Helpers/Helper.php"
],
The Error Is Call to undefined function show_name()
i ran Composer dump-autoload
but it's failed
Can Anyone Help Me Please
CodePudding user response:
Try to usefunction_exists
function
if (!function_exists('show_name')) {
function show_name()
{
return 'Ahmed Emmam';
}
}
CodePudding user response:
You should load the other files within the autoload
field.
{
"autoload": {
"psr-4": { ... },
"files": [
"app/Helpers/Helper.php"
]
}
}
https://getcomposer.org/doc/04-schema.md#files
CodePudding user response:
Aside from ensuring that your folders and files are located and named (case-sensitive) correctly, you likely want to add a namespace
directive to your Helper.php
file:
app/Helpers/Helper.php
<?php
namespace App\Helpers;
if (!function_exists('show_name')) {
function show_name() {
return 'Ahmed Emmam';
}
}
You also need to update your web.php
route to actually return a response:
routes/web.php
Route::get('test-helper' , function () {
return show_name();
});
Ensure everything is loaded correctly using composer dumpautoload
.