n Laravel - Custom Helper Facade Class | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Today, In this post i am going to share with you how to create custom global helper functions using Helper Facade in our laravel 5, laravel 6, laravel 7 and laravel 8 application. We always need to define some global functions for our application because that way we can access that helper functions from every file of our project.

Step 1 : Create Helper Class

In this step we will create new directory "Helpers" in App directory. After created Helpers folder we require to create create Helper.php file and put bellow code:

app/Helpers/Helper.php

<?php

namespace App\Helpers;

class Helper

{

public static function homePageURL()

{

return url('/');

}

}

 

Step 2 : Register Helper Class

In this step we have to register Helper class as facade in configuration file. So let's open app.php file and add Helper facade with load class. open app.php file and add bellow Helper facade line.

config/app.php

....

'aliases' => [

....

'Helper' => App\Helpers\Helper::class,

]

 

Step 3 : Create Route

We are going to create example from scratch so first we add new route "my-helper" for testing helper. So open your route file and add bellow route:

routes/web.php

Route::get('my-helper', function () {

  return view('myhelper');

});

 

Step 4 : Create View File

In this step we will create "myhelper.blade.php" file and we will class Helper facade function, so let's create myhelper.blade.php file and put bellow code.

resources/views/myhelper.blade.php

<!DOCTYPE html>

<html>

<head>

<title>My Helper Example</title>

</head>

<body>

<h2>My Helper Example</h2>

<p>{{ Helper::homePageURL() }}</p>

</body>

</html>

 

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/my-helper

I hope it can help you...

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook