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 😒😒😒
<?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%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
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:
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: