Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ITwrx's avatar
Level 1

my facade doesn't work in console command

I have a custom facade that works just fine in my controllers and models, but doesn't work in any artisan console command.

When using the normal "use" statement that works everywhere else ("use App\Facades\ITwrx\ITwrxCrypt;") in any console command, i get "A facade root has not been set.", but if i use the following path "use Facades\App\Facades\ITwrx\ITwrxCrypt;"(i don't understand why it needs to start with "Facades" when using from the console) or if i inject the facade into the handler using various methods, the latest being "$ITwrxCrypt = resolve('App\Facades\ITwrx\ITwrxCrypt');" and try to use the facade with "$ITwrxCrypt->decrypt(str_replace('ELOCRYPT', '', $value));" or "$value = ITwrxCrypt::decrypt(str_replace('ELOCRYPT', '', $value));", depending on which method i used above, i get "Call to undefined method App\Facades\ITwrx\ITwrxCrypt::decrypt()". So, to me, it seems the injection is working for my facade, but it doesn't "see" the base class my facade is extending. Maybe i'm being tricked though...

A little context: My ITwrxCrypt facade points to my custom service provider that extends laravel's encrypter class (class ITwrxEncryptionServiceProvider extends \Illuminate\Encryption\EncryptionServiceProvider). The "decrypt" method that is not being found is in laravel's "encrypter" class that my custom ITwrx_encrypter class is extending. Everywhere besides a console command, the same exact code with the decrypt method works fine.

how to get my artisan/console command to be able to use my facade (and the methods in the base class that it extends) like it does everywhere else?

If i need to pastebin the various pieces i can do that i was just trying to keep the verbosity under control.

thanks

0 likes
34 replies
laracoft's avatar

Please show the register() of your service provider which registers the facade.

ITwrx's avatar
Level 1

thanks for your reply.

//the service provider

public function register()
    {
        $this->app->singleton('ITwrx_encrypter', function ($app) {

            //various ITwrx stuffs...

            return new Encrypter($key, 'AES-256-CBC');        

        });
    }

//the facade

protected static function getFacadeAccessor()
    {
        return 'ITwrx_encrypter';
    }
laracoft's avatar

Your service provider facade name is registered as ITwrx_encrypter and it does not seem to tally with your earlier code.

Do make your code easier to read by wrapping them between ``` on a line by itself and use the markdown preview switch to preview before posting.

ITwrx's avatar
Level 1

sorry about that. so i think you're saying the "ITwrx_encrypter" in both places needs to be changed to "ITwrxCrypt" like the name of the facade. I'll try that now, and get back.

thanks

1 like
ITwrx's avatar
Level 1

unfortunately, i get the same result.

Call to undefined method App\Facades\ITwrx\ITwrxCrypt::decrypt()
laracoft's avatar

Sure, also, when quoting variables, wrap them between single ticks, ` to get this variable effect. Again, use preview to have a quick check. Easier to read, easier to solve. :)

laracoft's avatar

Do you have a public static function decrypt() function in your facade?

ITwrx's avatar
Level 1

no. that function is from the laravel encrypter that my encrypter extends. Which works everywhere else except console commands.

ITwrx's avatar
Level 1

and i see the decrypt function in Illuminate/Encryption/Encrypter is not a public static function but only a public function.

laracoft's avatar

Ok, my earlier public static function decrypt() was a wrong question.

  1. Do you have a class App\ITwrx\ITwrxCrypt with decrypt()?
  2. Is your facade class name App\Facades\ITwrx\ITwrxCrypt? What is the function getFacadeAccessor() returning?
laracoft's avatar

Generally, we have a simple class

namespace App;

class ITwrxCrypt{
    function decrypt()
    {
    }
}

And to facade it, our register it in our service provider

    $this->app->singleton('itwrxcrypt`', function ($app) {
        return new \App\ITwrxCrypt;
    });

And we further access the class via facade using

ITwrxCrypt::decrypt();
ITwrx's avatar
Level 1

i'll have to get back with you tomorrow. it's late here. thanks

1 like
ITwrx's avatar
Level 1
  1. no. i have App\Facades\ITwrx\ITwrxCrypt that does not contain decrypt() but decrypt() is supposed to be provided by Illuminate\Encryption\Encrypter via \Illuminate\Encryption\EncryptionServiceProvider (which my ITwrxEncryptionServiceProvider extends) . decrypt() works via my facade everywhere except when called from console commands. This is the main issue. My facade already works, but not from commands, because for some reason, the dependencies of my facade are not being injected too. That is the part i don't know how to fix. How to get the dependencies of my facade (namely, the \Illuminate\Encryption\EncryptionServiceProvider and the Illuminate\Encryption\Encrypter it makes available.
  2. yes. now i changed it to return ITwrxCrypt instead of ITwrx_encrypter, but in my experience, both work fine as long as both the getFacadeAccessor() in the facade returns the same name as the first line in my service provider.
ITwrx's avatar
Level 1

I've already got a working facade. This is not the problem, as far as i can tell. My problem, is that it doesn't work from the commands. This has something to do with dependency injection not working from the command as it does everywhere else in my app. It needs to be able to use decrypt() from Illuminate\Encryption\Encrypter(via my facade and service provider), which it is able to do everywhere except from commands.

thanks

ITwrx's avatar
Level 1

as a reminder, my service provider returns the Laravel Encrypter class (Illuminate\Encryption\Encrypter). I only do some things differently in my service provider before calling the standard encrypter. That encrypter is where the decrypt() function is.

laracoft's avatar

What happens when you do a app()->make('App\ITwrx\ITwrxCrypt') in your console? or for that matter, $o = new \App\ITwrx\ITwrxCrypt()?

Why do you need a facade?

ITwrx's avatar
Level 1

i'm not following you. I don't have a App\ITwrx\ITwrxCrypt. I have a App\Facades\ITwrx\ITwrxCrypt. I created the facade, so i can use my extended version of the laravel encrypter from anywhere in my app by just adding a use statement and calling ITwrxCrypt::function_of_laravel_encrypter which works, except in my artisan commands. Actually, the above usage is a minority use case, as the facade is mainly used by a package, but that is not related to my problem.

laracoft's avatar

I wrote some actual code to test out your approach.

In ITwrxEncryptionServiceProvider's register(), we tell Laravel what to do when asked to make a itwrxencryption, nothing to do with facade here.

        // Register the service the package provides.
        $this->app->singleton('itwrxencryption', function ($app) {
            return new \Your\Class;
        });

    public function provides()
    {
        return ['itwrxencryption'];
    }

When we call a function via facade, e.g. ITwrxEncryption::testing(), Laravel will locate the facade class and make using the string returned by getFacadeAccessor(), which is itwrxencryption, which leads back to our register() specifying what to do when asked to make the string itwrxencryption.

<?php

namespace App\ITwrxEncryption\Facades;

use Illuminate\Support\Facades\Facade;

class ITwrxEncryption extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'itwrxencryption';
    }
}

I did not have to include any use statement in my console command. In my console command, I was able to reach \Your\Class's testing() via ITwrxEncryption::testing();

ITwrx's avatar
Level 1

where did you place the testing() function? in the facade?

laracoft's avatar

I'm quite out of ideas, perhaps try a composer update to recreate the autoload classes.

laracoft's avatar

Not the facade, my testing() was in the class \Your\Class.

laracoft's avatar

What is the namespace and class name of your facade? As I understand it, Facades needs to be appended, not prepended.

Try using App\ITwrxCrypt\Facades as your namespace and ITwrxCrypt as your facade class name.

ITwrx's avatar
Level 1

my problem is that i need to be able to call ITwrxCrypt::decrypt() and it be able to "pull in" and use the decrypt() function from the laravel Encrypter via my service provider like it's able to everywhere else. Not so much being able to access my own function at App\ITwrx\Classname.

That being said, you've given me some more ideas on things to check and try, so i'll mess around some more, and see if i can figure it out.

thanks for your help.

ITwrx's avatar
Level 1

namespace App\Facades\ITwrx;

class ITwrxCrypt extends Facade

ITwrx's avatar
Level 1

i have my ITwrxCrypt facade in a dir named ITwrx that is in a dir named Facades that is in the app dir. I'm thinking my facade wouldn't work in the rest of the app if the namespace for the facade was wrong.

ITwrx's avatar
Level 1

using

use App\Facades\ITwrx\ITwrxCrypt;

$instance = ITwrxCrypt::getFacadeRoot();             
       
dd($instance);

from a controller returns an instance of Encrypter.php but if i use the same code in my command it returns null. Also, if i dump the app() service container from the command or the controller they both show the ITwrxCrypt binding and they both reference my service provider. In light of this, i have no idea why it doesn't work from the command. The docs say nothing of this discrepancy.

laracoft's avatar

It is a herculean task here to imagine and make assumptions about the code at your end. It's easier if you can paste your full

  1. class ITwrxEncryptionServiceProvider
  2. class Encrypter
  3. class ITwrxCrypt
  4. Console command class
ITwrx's avatar
Level 1

here's an updated version of everything.

https://paste.laravel.io/ac9d0bb4-c986-40ab-b362-16c22e83b682

hopefully, you, or anyone who knows more about the framework source than me, can tell me why my facade works from any controller i've tried, but not from my command. It also didn't work when called from app/Helpers/Helper.php. I tried that as a workaround, as Helper::whatever() works from my command, as do all models and everything else i've tried, except my facade, of course..

thanks for looking.

laracoft's avatar
laracoft
Best Answer
Level 27

@itwrx possible to paste the full codes? offhand, I can tell that boot() is missing from the service provider. It might be something else other than what you showed that cause the issue.

Next

Please or to participate in this conversation.