Reputation: 6135
Is there anyone who can suggest me how can we use helper in Lumen 8.0 version? Thanks a lot.
Upvotes: 3
Views: 2239
Reputation: 6135
I have followed below steps to add helper feature in my project in Lumen 8.0:
First Step: I have added "app/Helpers/MasterFunctionsHelper.php" string under "autoload" -> "files" array in composer.json
file. Here "MasterFunctionsHelper" is my helper name:
"autoload": {
"files": [
"app/Helpers/MasterFunctionsHelper.php"
],
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Second step: I have created "MasterFunctionsHelper.php" file at "app/Helpers/MasterFunctionsHelper.php". If "Helpers" directory is not exist then please create it in "app" directory.
Third step: created a class in "MasterFunctionsHelper.php" file:
<?php
namespace App\Helpers;
class MasterFunctionsHelper{
public static function sayhello()
{
return "Hello Friends";
}
}
Forth step: Opened controller file eg: "UsersController.php" and included "use App\Helpers\MasterFunctionsHelper;" and then called "MasterFunctionsHelper::sayhello();" function of helper class as:
<?php
namespace App\Http\Controllers;
use App\Helpers\MasterFunctionsHelper;
class UsersController extends Controller
{
public function index()
{
echo MasterFunctionsHelper::sayhello();
}
}
Fifth step: Opened command line and run below command:
composer dump-autoload
When I run "index" action of "UsersController" in the browser then output was as:
Hello Friends
It worked for me. I hope, this will also help you. Thank you.
Upvotes: 7