Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Learn how to create custom Facades in Laravel

    Learn how to create custom Facades in Laravel

    August 19, 2024

    Laravel Facades are a fantastic feature of the Laravel framework, providing convenient access to services via a simple interface. One thing that confused me when I was first learning Laravel was the facade accessor method.

    Once you understand that facades are a convenient way to access underlying services in Laravel’s service container, you can easily track the service behind ANY facade. Each facade provides a getFacadeAccessor() method that points to the registered service name.

    Here’s an example of the facade accessor for the DB Facade:

    // IlluminateSupportFacadesDB;

    protected static function getFacadeAccessor()
    {
    return ‘db’;
    }

    So, the ‘db’ string points to a service in the container, which the facade will use to resolve. If you jump over to a tinker session, you can try it out for yourself:

    Some Facades might resolve to a manager class, which will then dynamically pass methods to an underlying macro or database connection class:

    public function __call($method, $parameters)
    {
    if (static::hasMacro($method)) {
    return $this->macroCall($method, $parameters);
    }

    return $this->connection()->$method(…$parameters);
    }

    When called without a connection, the DatabaseManager will use the default connection (sqlite in my example above), and call methods on that connection.

    I recommend reading how facades work in the Laravel documentation, which also has a list of Facade references that are available through the framework.

    Creating Your Own Facades in Laravel

    Why would you ever want to create a Facade within your application if you’re not creating a plugin or working on the Laravel framework directly? Some developers prefer to only use dependency injection for services defined in the application code, and that’s fine. However, I find it convenient to define Facades for services I use frequently either through helpers or an AppFacades namespace. I enjoy Laravel’s flexibility while still laying down conventions to be productive in an unfamiliar codebase.

    To create a Facade within your application code, I’d recommend the make:class command to generate a Facade, which you can do as follows:

    php artisan make:class App/Facades/Example

    Let’s say you defined a service in your app service provider called AppExampleService; you could then create a Facade for it after generating the class:

    namespace AppFacades;

    use IlluminateSupportFacadesFacade;

    class Example extends Facade
    {
    public static function getFacadeAccessor()
    {
    return ‘example_service’;
    }
    }

    You could just as easily make the Facade accessor the string of the fully qualified class if you didn’t define an alias or use a string to define it in the service provider:

    public static function getFacadeAccessor()
    {
    return AppExampleService::class;
    }

    Facades have the nice convenience of directly mocking the underlying service class in a test:

    use AppFacadesExample;

    Example::shouldReceive(‘getLatestPosts’)
    ->with($after_date)
    ->andReturn($test_posts);

    If you were not using the Facade, Laravel provides other convenient mocking methods like partialMock() to conveniently swap a service for a mock in a test. With the service, you could do this directly in a test like so:

    $mock = $this->partialMock(MyApiService::class, function (MockInterface $mock) {
    $mock->shouldReceive(‘getLatestPosts’)
    ->with($after_date)
    ->andReturn($test_posts);
    });

    It is up to you to decide the approach you want to take, but if you make heavy use of a service in your application, consider creating a Facade to provide some convenience around using your service without the loss of features dependency injection offers.

    The post Learn how to create custom Facades in Laravel appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleSome of the announcements coming to Laracon US
    Next Article Perficient Named a 2024 Top Workplace in Atlanta

    Related Posts

    Machine Learning

    Salesforce AI Releases BLIP3-o: A Fully Open-Source Unified Multimodal Model Built with CLIP Embeddings and Flow Matching for Image Understanding and Generation

    May 16, 2025
    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 16, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    iPad Pro (2024) review: Apple’s most advanced tablet is no MacBook, and I’m okay with that

    Development

    In conversation with AI: building better language models

    Artificial Intelligence

    How to Set Semantic Versioning for .NET Core Apps and Libraries

    Development

    Build Design Systems With Penpot Components

    Development

    Highlights

    CVE-2025-26262 – R-fx Networks Linux Malware Detect Arbitrary Code Execution and Privilege Escalation

    May 6, 2025

    CVE ID : CVE-2025-26262

    Published : May 6, 2025, 5:15 p.m. | 2 hours, 19 minutes ago

    Description : An issue in the component /internals/functions of R-fx Networks Linux Malware Detect v1.6.5 allows attackers to escalate privileges and execute arbitrary code via supplying a file that contains a crafted filename.

    Severity: 0.0 | NA

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    How Aetion is using generative AI and Amazon Bedrock to translate scientific intent to results

    February 6, 2025

    CVE-2025-37792 – Linux Bluetooth btrtl NULL Pointer Dereference Vulnerability

    May 1, 2025

    The Secret Sauce to Winning Clients Worth $25K Daily: ChatGPT + LinkedIn + Quora

    May 22, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.