I am trying to call middle-ware in constructor of my controller.
My PostController class is below
class PostController extends Controller
{
public function __construct()
{
$this->middleware( ['auth:admin', ['only'=> ['store', 'update']]], ['auth:client', ['only'=> ['index', 'view']]]);
}
}
Please suggestion or correct me if I am wrong.
I think the best way do it in routes
Route::post('path', 'IndexController@store')->middleware(['auth:admin']);
Route::get('path', 'IndexController@index')->middleware(['auth:client]);
Or in group, for example:
Route::group(['middleware' => ['auth:admin']], function ($route) {
$route->post('storePath', 'IndexController@store');
$route->put('updatePath', 'IndexController@update');
});
Yes, you can call the middleware function multiple times.
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth:admin', ['only'=> ['store', 'update']])
$this->middleware('auth:client', ['only'=> ['index', 'view']]);
}
}