Rbex
Rbex

Reputation: 1539

Route User Role Laravel 5.4

I'm very confused on this situation. I have two routes with on resource name.

Route::resource('product', 'Product\AreaManagerProductController');
Route::resource('product', 'Product\SystemAdminProductController');

I need to make it as one because I have a contextual binding.

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

           $this->app->when(SystemAdminProductController::class)
          ->needs(ProductInterface::class)
          ->give(SystemAdminProductRepository::class);

The contextual binding works fine... but I need to change my route like this.

Route::resource('product_area_manager', 'Product\AreaManagerProductController');
Route::resource('product_system_admin', 'Product\SystemAdminProductController');

I created ProductController and some kind of weird solution.

public function index(){

        //Create a conditional statement base on user

        return app('App\Http\Controllers\Product\AreaManagerProductController')->index();

    }

It may work but it doesn't trigger the middleware... What could be the best practice on this situation. TY

Upvotes: 0

Views: 129

Answers (1)

Reiah Paul Sam
Reiah Paul Sam

Reputation: 555

You can have your Route like this

Route::group(['prefix' => 'product', 'namespace' => 'Product', 'middleware' => '<your middleware>'], function() {

        Route::resource('area_manager', 'AreaManagerController');
        Route::resource('system_admin', 'SystemAdminController');
});

The reason I grouped the route is to reduce redundancy, and the reason i removed Product from the controller name is, as there is a namespace Product already, there is no need of long Class names.

If you wan to access some methods in the AreaManagerController and SystemAdminController just extend the ProductController to these Controllers.

If you want to add some specific middleware for the actions inside these controllers, I have added a middleware clause in the route group which will affect to these controllers, if not needed just remove it.

Hope this helps you.

Upvotes: 3

Related Questions