简体   繁体   中英

How do I over ride (new class) a facade in Laravel 5.4?

I've get the following class

<?php

namespace App;


class Currency extends \Casinelli\Currency\Currency
{

    /**
     * Create a new instance.
     *
     * @param \Illuminate\Foundation\Application $app
     */
    public function __construct($app)
    {
        parent::__construct($app);
        $this->setCurrency(getCurrency());
    }
}

I've replaced the 'aliases' in app.php :

   - 'Currency' => \Casinelli\Currency\Facades\Currency::class,
   + 'Currency' => \App\Currency::class,

However, I'm running into an error of:

Non-static method Casinelli\\Currency\\Currency::rounded() should not be called statically

It seems my Currency class is not being treated as a Facade ... How would I go about resolving this?

Before answering, I recommend to fork the package, do your modifications and use your fork. Because if something change in the package, your override may not work anymore.

Let's take a look at the package.

You have 3 important files :

The currency that you want to extend: https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/Currency.php

The facade that you want to use : https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/Facades/Currency.php

And finally, the ServiceProvider that register the class you want to extends: https://github.com/Casinelli/Currency/blob/master/src/Casinelli/Currency/CurrencyServiceProvider.php#L60

The service provider will register the class Currency as a singleton with the alias of currency

Then, when you call the facade Currency, it will look for the alias currency and return a the instance of the class Currency.

Implement your own Currency

To use your own Currency class , you will need to register your own implementation of the Currency class in a service provider that will replace the service provider of the package.

  1. Create your own serviceProvider
    $ php artisan make:provider ExtendedCurrencyServiceProvider

  2. In your file app/config/app.php ,
    Replace Casinelli\\Currency\\CurrencyServiceProvider::class,
    with App\\Providers\\ExtendedCurrencyServiceProvider::class,

  3. In your new service provider change to this
<?php

namespace App\Providers;

use Casinelli\Currency\CurrencyServiceProvider;

class ExtendedCurrencyServiceProvider extends CurrencyServiceProvider
{
     /**
     * Register currency provider.
     */
    public function registerCurrency()
    {
        $this->app->singleton('currency', function ($app) {
            return new App\Yournamespace\CurrencyClass($app);
        });
    }
}
  1. Laravel 5.5+ In your composer.json remove the service provider from the autodiscovery
"extra": {
    "laravel": {
        "dont-discover": [
            "Casinelli\\Currency\\CurrencyServiceProvider"
        ]
    }
},

Now, when you will call \\Currency::rounded() it will call your own implementation of the currency.

You don't need to change the Facade.

The error message / exception is very misleading...

The issue was due to calling App\\Currency::rounded instead of: Casinelli\\Currency\\Facades\\Currency::rounded ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM