i want to create a functions which can be called by all controllers
public function getSuffix($filename) {
$first_char = substr($filename, 0 ,1);
$sec_char = substr($filename, 1 ,1);
$suffix ="";
if($first_char!='.' && $first_char!='..')
$suffix .= '/'.$first_char.'/';
if($sec_char!='.' && $sec_char!='..')
$suffix .= $sec_char.'/';
return $suffix;
}
what is the best practice for doing this? Is helper class what i'm searching?
create a helpers.php file inside app/http folder. then your composer.json file autoload this file .
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Http/helpers.php"
]
},
then run composer dump-autoload your problem is solved
First you need to create folder in app like CustomFolder and create you helper class in custom folder like SimpleClass.php
<?php
namespace App\CustomFolder;
class SimpleClass {
public function yourFunction(){
return true;
}
}
?>
when you use this helper class in your controller simple add namespace like
<?php
use App\CustomFolder\SimpleClass;
class MyController extends Controller{
}
?>
I think your helper class work perfectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With