0

I want to bind same class with different type of classes depending on Controller name , for example

$this->app->bind(BaseFormRequest::class,function(){
            return new CategoryFormRequest;
        });
$this->app->bind(BaseFormRequest::class,function(){
            return new TagFormRequest;
        });

I want CategoryFormRequest to be bind instead of BaseFormRequest if it was use in CategoryController, same as TagFormRequest to be bind instead of BaseFormRequest if it was use in TagController

I have used when() but it doesn't work as expected

 $this->app->when(CategoryController::class)
        ->needs(BaseFormRequest::class)
        ->give(function(){
            return new CategoryFormRequest();
        });

Thanks !

  • You never do this on controllers, you always have the same Form Request for the same method, why do you want to make this dynamic? This is a code smell, sort of a red flag that you are doing something wrong – matiaslauriti Sep 28 '22 at 22:29
  • Behind the scene you are right in one point. However, the two controllers extend other main controller with that method. Meanwhile the method are applicable for two controllers and I want to change the parameter of that method depending on type of controller. I could use dependency inversion in the body of that method but I want to change dependency in the parameter.. Hope you understand – Hassan Elshazly Eida Sep 28 '22 at 22:36
  • I think I do understand, but there is still a code smell. You should never be extending controllers (if you need methods that are not going to return a response, just use traits or normal classes). Could you share an example of those controllers so I understand exactly what you want? I am saying this because I think the code is not right, it should be resolved in other ways, but I understand if you cannot change the code. This is just my tip and how I can be of help, hope you do understand – matiaslauriti Sep 29 '22 at 13:36

0 Answers0