Laravel - Custom Helper Facade Class Example from scratch (ok)

https://www.itsolutionstuff.com/post/laravel-5-custom-helper-facade-class-example-from-scratchexample.html

Thực sự chưa hiểu rõ lắm về Facade nhưng theo ý hiểu hiện tại nó dùng để gọi đúng class twhcj sự cần còn không ở những đường dẫn khác không cần thiết (ok) Do đó với cách sử dụng như thế này nó sẽ tốt hơn với cách sử dụng Custom Helper Facade Class rất nhiều, Custom Helper Facade nó dụng mọi nơi 😒😒😒 đọc bài viết để biết thêm: "How to create custom facade in laravel 5.2?"

C:\xampp\htdocs\reset\config\app.php

<?php
return [
    'aliases' => [
       ....
        'Helper' => App\Helpers\Helper::class,
    ],
];

C:\xampp\htdocs\reset\app\Helpers\Helper.php

<?php
namespace App\Helpers;
class Helper {
  public static function homePageURL() {
    return url('/');
  }
}

C:\xampp\htdocs\reset\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>

C:\xampp\htdocs\reset\routes\web.php

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */
use App\Http\Controllers\HomeController;
Route::get('/', function () {
  dd('Welcome to simple user route file.');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('my-helper', function () {
    return view('myhelper');
});

Laravel - Custom Helper Facade Class Example from scratch

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

<?phpnamespace 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:

Read Also: Laravel - Simple user access control using Middleware

http://localhost:8000/my-helper

I hope it can help you...

Last updated