I have a route setup like so:
Route::prefix('/admin/{company}')->group(function() {
Route::resource("employees", "Admin\EmployeesController");
Route::resource("jobs", "Admin\JobsController");
// and few other routes
})->middleware('admin');
Before, I was using {companyId} but I want to change it for model binding now. And my middleware was checking through $companyId = $request->route('companyId');
public function handle($request, Closure $next)
{
$id = $request->route('companyId');
$adminRole = Role::where('name', 'admin')->first();
if (!Auth::user()->userRoles->where('company_id', $id)->where('role_id', $adminRole->id)->count()) {
return response("Unauthorized action", 403);
}
return $next($request);
}
As I am refactoring for using Model Binding {company} instead of {companyId} parameter, I realised stuff like Gates and Policies. Do I need to utilise Gates or Policies to make it the good way?
However, I probably don't want to make it for each one of my route or each one of my resources as /admin prefix is already defined there. Is there a way to put a gate or policy based on the prefix?
What is a good practice to make the code look nicer?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire