Laravel Make Facades

Laravel Make Facades Package


Overview

    • Description and Screencast

    • Install Package

    • Publish Package

    • Configure Package

    • Package Configuration Options

    • Make Facades (Using Package artisan command)

    • Default ServiceClass Scaffold Breakdown

    • Default ServiceClassFacade Scaffold Breakdown

    • Important Note about auto_alias_facades

    • In Closing


Description

Clean Code Studio package to generate Facades in a single command - php artisan make:facade MyCoolService

Installation


composer require clean-code-studio/laravel-make-facades --dev

    • Install package via composer in your terminal


Publish


php artisan vendor:publish

Select: CleanCodeStudio\MakeFacades\ServiceProvider



Define Service Config Settings

config/make-facades.php

    • config.make-facades.path

      • Defines path to the facades folder path

        • AKA where new facades will be created to

    • config.make-facades.namespace

      • Defines namespace on every Facade class generated by this package.

    • config.make-facades.providers_path

      • New setting option to support Laravel 8

      • @see Issue #4 if you have issues with Laravel 8

    • config.make-facades.auto_alias_facades

      • Defines whether a facade's service class will automatically bind to the service container.

      • If this is false you will have to manually bind service classes to the service container within any service provider for each service class' Facade to properly work.


Default Service Config Settings

config/make-facades.php

return [

// Directory path save your facades

'path' => 'app/facades',


// Namespace Of Your Facades

'namespace' => 'App\\Facades',


// Providers path (@see https://github.com/zhorton34/laravel-make-facades/issues/4)

'providers_path' => 'app/Providers',

// This will find all of the aliases and services defined in your path settings and

// 1. Bind the service classes for each facade to the service container automatically

// 2. Register aliases for each facade base on the Class Name the Facade Reference to the service container automatically

'auto_alias_facades' => true,

];


Make Facades

php artisan make:facade MyCoolService

    • Creates Scaffold for MyCoolService class

    • Creates Scaffold for MyCoolServiceFacade class

    • Note:

    • If 'auto_alias_facades' => true in config/make-facade.php then the service will automatically be binded to your service container

    • If 'auto_alias_facades' => false in config/make-facade.php then you need to bind your generated service class to the service container in any service provider for the facade to properly work.


Default ServiceClass Scaffold

Default creation format: App\Facades\MyCoolService\MyCoolService.php

<?php


namespace App\Facades\MyCoolService;


class MyCoolService

{

// create MyCoolService class

}


Default ServiceClassFacade Scaffold

By Default created at App\Facades\MyCoolService\MyCoolServiceFacade.php

<?php


namespace App\Facades\MyCoolService;


use Illuminate\Support\Facades\Facade;


class MyCoolServiceFacade extends Facade

{

protected static function getFacadeAccessor()

{

return 'MyCoolService';

}

}


Important Note

If 'auto_alias_facades' => false in config/make-facade.php then you need to

Bind generated service class (Example MyCoolServiceClass) to the service container whenever you create a new facade and service scaffold via running the make:facade command.

If auto_alias_facades is set to true then it will register the service class (Example MyCoolServiceClass) to the service container automatically.


Closing


Notes

  • A. Remember You Can Auto Bind The Services and Auto Aliases Your Service Facades Simply By Setting auto_alias_facades to TRUE within you config/make-facades.php file! Simple, quick, and easy.

  • B. If you do change your namespace or file path in your config, Facades and Facade Services within the old namespace and old file path WILL NOT AUTOMATICALLY REGISTER TO THE CONTAINER.

  • C. If you do not want to auto register or automatically add the aliases for your generated facades, simply set auto_alias_facades to false within your config/make-facades.php file


Open Service Config File


config/make-facades.php

    • Note: config/make-facades.php is created during the previous step when we publish this package via artisan