Laravel 5 - Custom Helper Facade Class Example from scratch

By Hardik Savani | March 5, 2017 | | 20011 Viewer | Category : PHP Laravel


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 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.

In this tutorial i am going to share with you how we can make global helper function in Laravel framework from scratch. It is very important to create custom helper functions we have before start our projects. In this post we learn how to create custom Helper facade, i also posted few days ago "How to create custom helper in Laravel 5?", in that post you can create custom helpers file.

Ok, now we proceed to create custom helper facade to create helper functions, we have to just follow few bellow step to create helper facade.

1) Install Laravel Fresh Application

2) Create Helper Class

3) Register Helper Class

4) Create Route

5) Create View File

Ok so, let's proceed with above steps in brief.

Step 1 : Install Laravel Fresh Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2 : 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 3 : 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 4 : 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 5 : 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.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...



Do you want to expert php developer or developer team for your projects ?
Contact US
We are Recommending you: