skip to Main Content

On laravel site I defined a facade based on LoggedUserService with Interface class, whicj looks like :

<?php

namespace AppLibraryFacades;

use AppLibraryServicesInterfacesLoggedUserInterface;
use IlluminateFoundationAuthUser as Authenticatable;

class LoggedUserFacade
{
    public static function getAvatarHtml(?string $customClass = '', bool $showPermissions = false): string
    {
        $loggedUserInterface = app(LoggedUserInterface::class);
        return $loggedUserInterface->getAvatarHtml(customClass: $customClass, showPermissions: $showPermissions);
    }

    public static function checkUserLogged(): string
    {
        $loggedUserInterface = app(LoggedUserInterface::class);
        return $loggedUserInterface->checkUserLogged();
    }

    public static function getLoggedUser(): Authenticatable|null
    {
        $loggedUserInterface = app(LoggedUserInterface::class);
        return $loggedUserInterface->getLoggedUser();
    }


}

I defined several public static functions and wonder if there is a way to make one app bind calling, not in any static method I have above ?

"laravel/framework": "^10.48.7",
"php": "8.2",

Thanks in advance!

Attempt To Fix :

No, I got error :

Constant expression contains invalid operations

with such declaration.

I tried to init in __construct method :

class LoggedUserFacade
{
    private static $loggedUserInterface;

    public function __construct()
    {
//        die("IF UCOMMENTED - IS NOT CALLED");
        self::$loggedUserInterface = app(LoggedUserInterface::class);
    }

    public static function getAvatarHtml(?string $customClass = '', bool $showPermissions = false): string
    {
        return self::$loggedUserInterface->getAvatarHtml(customClass: $customClass, showPermissions: $showPermissions);
    }

But this __construct method is not called…

2

Answers


  1. You can use a facade like this :

    <?php
    
    namespace AppLibraryFacades;
    
    use AppLibraryServicesInterfacesLoggedUserInterface;
    use IlluminateFoundationAuthUser as Authenticatable;
    
    class LoggedUserFacade extends Facade
    {
        
        /**
         * Get the registered name of the component.
         *
         * @return string
         */
        protected static function getFacadeAccessor() { return 'logged-user-service'; }
    
    }
    

    You have to create a service provider LoggedUserServiceProvider.php :

    class LoggedUserServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            $this->app->singleton('logged-user-service', function()
            {
                return new LoggedUserService;
            });
        }
    }
    

    Assuming that the service that implement LoggedUserInterface is LoggedUserService.

    And don’t forget to register your service provider :

    'providers' => ServiceProvider::defaultProviders()->merge([
        // Other Service Providers
    
        AppProvidersLoggedUserServiceProvider::class,
    ])->toArray(),
    

    For the usage you simply do it like this :

    LoggedUserFacade::getAvatarHtml($customClass, $showPermissions);
    LoggedUserFacade::checkUserLogged();
    LoggedUserFacade::getLoggedUser();
    
    Login or Signup to reply.
  2. You can actually implement your own facade using a Laravel’s facade

    namespace AppLibraryFacades;
    
    use AppLibraryServicesInterfacesLoggedUserInterface;
    use IlluminateSupportFacadesFacade;
    
    
    class LoggedUserFacade extends Facade {
       protected static function getFacadeAccessor() {
           return LoggedUserInterface::class;
       }
    }
    

    You can then use the facade by just using method names defined in LoggedUserInterface e.g.

       LoggedUserFacade::checkUserLogged();
    

    Laravel will handle injecting the interface as a singleton based on how you’ve bound it in the DI container.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search