dani94
3 days ago

Help with Service Container

Posted 3 days ago by dani94

Hey guys!

I am helping to develop a laravel opensource package, but Im a bit confused trying to auto solve dependency inyection.


class MyBaseClass extends FormRequest
{
    protected $model;
    protected $overflow_column;

    public function __construct(Model $model, $overflow_column = "properties")
    {
        $this->model = $model;
        $this->overflow_column = $overflow_column;
    }

   //methods
    ....
}


class MyCustomClass extends MyBaseClass
{
    public function __construct(TestModel $model)
    {
        parent::__construct($model);
    }
}

Im dont know how to auto inyect in custom class which extends from MyBaseClass a laravel Model. (Yes, I read binding doc).

I would like to do something like that:

class UserFormRequest extends MyBaseClass
{
    public function __construct(User $user)
    {
        parent::__construct($user);
    }
}
public class UserController
{
    public function show(UserFormRequest $request)
    {
       // here I can use MyBaseClass methods 
    }
}

I tried (didnt work) manually binding, but I need an auto binding.


public class CustomServiceProvider
{

   public function register()
   {
      $this->app->bind(MyCustomClass::class, function ($app) {
            return new MyCustomClass($app->make(TestModel::class));
        });
   }

}

Thanks!!!

Please sign in or create an account to participate in this conversation.