I want to create a custom function for my Policy but I don't know how to use it in the route as middleware or something. Basically below is the code for my policy
class DepartmentPolicy
{
use HandlesAuthorization;
public function list(User $user, Department $department)
{
return true;
}
}
Now I want to call this in my Route something like this
Route::prefix('/departments')->group(function () {
Route::get('/list', [App\Http\Controllers\DepartmentController::class, 'list'])
->middleware('can:list,department');
});
But I always get a 403 function. When I remove this middleware I can access the url. But with this I get a 403.
Note: I already created the policies in the AuthServiceProvider like below
protected $policies = [
'\App\Models\User' => 'App\Policies\UserPolicy',
'\App\Models\Department' => 'App\Policies\DepartmentPolicy',
];
Can anyone tell me how to use this custom function in my custom route? Basically I know that by default if I access the index
in the controller it will call the viewAny
function in my DepartmentPolicy but what about the custom function in the policy and the custom route? how can I call that or link each other?