Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I override a method in Toaster class #121

Closed
thaqebon opened this issue Jul 5, 2021 · 5 comments
Closed

How can I override a method in Toaster class #121

thaqebon opened this issue Jul 5, 2021 · 5 comments

Comments

@thaqebon
Copy link

thaqebon commented Jul 5, 2021

Hi,

I want to make a bit modification on "setDefaultConfig" and "middleware" methods in Toaster class.
How I can do that and where should I place my modification without touch the original files.

Sorry to post this here if it not related to issues.
Thanks

@thaqebon
Copy link
Author

thaqebon commented Jul 5, 2021

Also I whould like to shere my modification, It may help someone.

In setDefaultConfig method I added 'showClass' & 'hideClass', So I can set a default animation.
In middleware method, It unset 'heightAuto', 'width', 'padding' without any way to reset, So I remove them.

@realrashid
Copy link
Owner

Hey @o4itea ,

It's very simple just like we override any parent class function in derived class.

You can try following :

Create a child class extending Toaster class and override the  "setDefaultConfig" and "middleware" methods.

use RealRashid\SweetAlert;

Class ChildClass extends Toaster {

    public function setDefaultConfig () {
       // Write your logic here
    }

    public function middleware () {
       // Write your logic here
    }

}

Hope this will help.

@thaqebon
Copy link
Author

thaqebon commented Jul 6, 2021

Thank Bro,
Where should I put the child class how to tell laravel to override parent class

@realrashid
Copy link
Owner

Create the Service Provider for overridden class.
First parameter of bind function must be same as facade accessor of sweet-alert Facade Check Here. Register this facade in config/app.php after the service provider of sweet-alert package. :

use RealRashid\SweetAlert;
use app\ChildClass;
Class ChildClassServiceProvider extends ServiceProvider {
    public function register(){
        $this->app->bind('alert', function(){
           return new ChildClass($this->app['config']);
        })
    }
}

Complete Code:

// ChildClass

use RealRashid\SweetAlert;

Class ChildClass extends Toaster {

    public function setDefaultConfig () {
       // Write your logic here
    }

    public function middleware () {
       // Write your logic here
    }

}
// ServiceProvider

use RealRashid\SweetAlert;
use app\ChildClass;
Class ChildClassServiceProvider extends ServiceProvider {
    public function register(){
        $this->app->bind('alert', function(){
           return new ChildClass($this->app['config']);
        })
    }
}

@thaqebon
Copy link
Author

Thanks Rashid, You've lighted the way in front of me.

The following code works

// ServiceProvider

use App\ChildClass;
Class ChildClassServiceProvider extends ServiceProvider {
    public function register(){
        $this->app->bind('alert', ChildClass::class)
    }
}

or replace the facade with the targeted class

// ServiceProvider

use RealRashid\SweetAlert\Toaster;
use App\ChildClass;
Class ChildClassServiceProvider extends ServiceProvider {
    public function register(){
        $this->app->bind(Toaster::class, ChildClass::class)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants