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 😒😒😒
By Hardik Savani February 10, 2016 Category : LaravelPlayUnmuteLoaded: 1.01%FullscreenSometimes 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
Step 2:Create ServiceProvider
In this step you should create service provider for bind class, so fire your terminal or cmd this following command:
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
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:
Step 4:Register Service Provider
Simple Register your service provider in Config\app.php like this way :
And also add aliase for facade in this file like this way :
Step 5:composer dump
This is the last step and you have to do just composer dump-autoload in your terminal:
<?php
namespace App\ItSolution;
use Illuminate\Support\Facades\Facade;
class DemoClassFacade extends Facade {
protected static function getFacadeAccessor() {
echo "aa2";
return 'democlass';
}
}
?>
<?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);
}
}
?>
<?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);
});
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); }}