Home > Net >  Automatically run and return when resolving a class?
Automatically run and return when resolving a class?

Time:11-26

I have a service that is injected into my controller:

public function store(\App\Services\PolymorphicService $poly) {

    $poly->resolveClass();

Inside this service class I have some code that resolves POST data and returns a specific class.

For example:

resolveClass() {
     // do logic
     // return class
}

Is there a way to automatically run the method poly->resolveClass(); and have that returned on the injection part:

public function store(\App\Services\PolymorphicService $poly) {

Instead of having to manually call my method?

CodePudding user response:

Place your method call inside the __construct() method of your service class, then it will be called automatically whenever your service is used or anything is done related to that service.


__construct(\App\Services\PolymorphicService $poly){
    $poly->resolveClass();
}

CodePudding user response:

You can create event listeners which fires every time the model is saved. See for example: Where should I put model saving event listener in laravel 5.1

  • Related