How to create custom facade in laravel 5.2? (ok)

https://www.itsolutionstuff.com/post/how-to-create-custom-facade-in-laravel-52example.html

ONói một cách đúng đắng việc đăng ký một Facade là việc đăng ký sử dụng một class (ok)

Sau đây là một cách đăng ký một fadece 👏

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

'providers' => [
    ...
    App\Providers\DemoClassServiceProvider::class,
],
'aliases' => [
    ...
    'DemoClass'=> App\ItSolution\DemoClassFacade::class
]

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:\xampp\htdocs\reset\app\Providers\DemoClassServiceProvider.php

<?php
namespace App\Providers;
use App;
use Illuminate\Support\ServiceProvider;
class DemoClassServiceProvider extends ServiceProvider {
  /**
   * Bootstrap the application services.
   *
   * @return void
   */
  public function boot() {
    //
  }
  /**
   * Register the application services.
   *
   * @return void
   */
  public function register() {
    echo "aa1";
    App::bind('democlass', function () {
      return new \App\ItSolution\DemoClass;
    });
  }
}

C:\xampp\htdocs\reset\app\ItSolution\DemoClassFacade.php

<?php
namespace App\ItSolution;
use Illuminate\Support\Facades\Facade;
class DemoClassFacade extends Facade {
  protected static function getFacadeAccessor() {
  	echo "aa2";
  	return 'democlass';
  }
}
?>

C:\xampp\htdocs\reset\app\ItSolution\DemoClass.php

<?php
namespace App\ItSolution;
class DemoClass {
  public function productImagePath($image_name) {
  	echo "aa3";
    return public_path('images/products/' . $image_name);
  }
  public function changeDateFormate($date, $date_format) {
    return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format($date_format);
  }
}
?>

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('democlass', function () {
  $imagepath = DemoClass::productImagePath('image.jpg');
  print_r($imagepath);
});

How to create custom facade in laravel 5.2?

By Hardik Savani February 10, 2016 Category : LaravelPlayUnmuteLoaded: 1.01%FullscreenVDO.AISometimes we are use repeat code and repeatedly use function at that time we can create custom facade for our project, that way you can create functions and use it.So, How to generate custom facade, I give you the few step to create custom facade:

Step 1:Create class file app/ItSolution/DemoClass.php

In Following step, first you should create "ItSolution" directory in app folder and create file DemoClass.php(app/ItSolution/DemoClass.php).now copy the following code in your DemoClass.php file.

app/ItSolution/DemoClass.php

namespace App\ItSolution;class DemoClass {    public function productImagePath($image_name)    {        return public_path('images/products/'.$image_name);    }    public function changeDateFormate($date,$date_format){		return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)					->format($date_format);	}}

Step 2:Create ServiceProvider

In this step you should create service provider for bind class, so fire your terminal or cmd this following command:

php artisan make:provider 'DemoClassServiceProvider'

Ok, now you can find new file DemoClassServiceProvider.php in your providers(app/Providers) directory.So, Basically open app/Providers/DemoClassServiceProvider.php and copy the following code:

app/Providers/DemoClassServiceProvider.php

namespace App\Providers;use Illuminate\Support\ServiceProvider;use App;class DemoClassServiceProvider extends ServiceProvider{    /**     * Bootstrap the application services.     *     * @return void     */    public function boot()    {        //    }    /**     * Register the application services.     *     * @return void     */    public function register()    {         App::bind('democlass', function()        {            return new \App\ItSolution\DemoClass;        });    }}

Step 3:Create Facade Class

In this step create another class app/ItSolution/DemoClassFacade.php, In this class you have to extend "Illuminate\Support\Facades\Facade" class, copy following link:

namespace App\ItSolution;use Illuminate\Support\Facades\Facade;class DemoClassFacade extends Facade{    protected static function getFacadeAccessor() { return 'democlass'; }}

Step 4:Register Service Provider

Simple Register your service provider in Config\app.php like this way :

// In providers arrayApp\Providers\DemoClassServiceProvider::class,

And also add aliase for facade in this file like this way :

// In aliases array'DemoClass'=> App\ItSolution\DemoClassFacade::class

Step 5:composer dump

This is the last step and you have to do just composer dump-autoload in your terminal:

composer dump-autoload

At Last you can use this way:

Read Also: Laravel Create Custom Artisan Command with Example

Route::get('democlass', function(){    $imagepath = DemoClass::productImagePath('image.jpg');    print_r($imagepath);});

Last updated