Monday, May 22, 2017

Route Controller and Contextual Binding Laravel 5.4

I have a problem in my route. How can I call either AreaManagerProductController or AdminProductController using route resource. This one Route::resource('product', 'Product\ProductController'); will trigger ProductRepository and that is correct.

If I want to trigger AreaManagerProductController I need to Route::resource('productareamanager', 'Product\AreaManagerProductController');

and for Admin Route::resource('admin', 'Product\AdminProductController');.

Is there a better way to do this? I want to trigger only the Route::resource('product', 'Product\ProductController'); and in there I can use either AreaManagerProductController or AdminProductController

My ProductController

class ProductController extends Controller
{

     public function __construct(ProductInterface $product){

        $this->product = $product;
    }
}

My ProductServiceProvider

 $this->app->when(ProductController::class)
          ->needs(ProductInterface::class)
          ->give(ProductRepository::class);

 $this->app->when(AreaManagerProductController::class)
          ->needs(ProductInterface::class)
          ->give(AreaManagerProductRepository::class);

 $this->app->when(AdminProductController::class)
          ->needs(ProductInterface::class)
          ->give(AdminProductRepository::class);



via Rbex

Advertisement