Rohit Urane
Posted on May 3, 2024
In this article, We are studying facades in laravel. A facade is a design pattern that provides a static interface to classes inside the framework's service container. You can access all the features of laravel with facades. It provides the benefit of a terse, expressive syntax while maintaining more testability and flexibility.
Some of the laravel facades
- Cache: Access the caching system.
- Config: Access the configuration values.
- DB: Interact with the database using Laravel's query builder
- Log: write a log message
- Mail: Access the Mailing system.
- File: Perform file operation
- Route: handle HTTP request
- Storage: handle file system.
- Session: Manage session data
Laravel contains many facades to handle the core functionality of laravel. Also, you can create custom facades that allow you to access them more concisely throughout your application.
How to create a custom Facade in Laravel
Create a helper class
Create a "Message" directory inside the App directory and create a php class inside the directory.
<?php
namespace App\Message;
class WelcomeMessage
{
public function greet()
{
return 'Welcome to Our Platform';
}
}
Register helper class
You can register a helper class in AppServiceProvider Provider.
public function register()
{
$this->app->bind('greeting', function(){
return new WelcomeMessage();
});
}
You can read the article on website
Posted on May 3, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.