You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a question regarding to use Laravel policy. After registering the policy into AuthServiceProvider, should it called automatically or it should be invoked through middleware, controller?
Example:
I have created a Product model and ProductPolicy classes. then I registered my ProductPolicy into AuthServiceProvider as below.
Route::middleware('auth:api')->group( function () {
Route::resource('products', \App\Http\Controllers\ProductController::class);
});`
ProductController.php
`class ProductController extends BaseController
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
return $this->sendResponse($products, 'Products retrieved successfully.');
}`
Now, when I consumed the below curl request, the ProductPolicy is not invoked. As you can see I use the Laravel Log just to make sure that my policy is invoked.
So, could you please help me to understand when the policy will be invoked? and if there is anyway to invoke it when any model action is happening ( searching, create, update etc .. ).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have a question regarding to use Laravel policy. After registering the policy into AuthServiceProvider, should it called automatically or it should be invoked through middleware, controller?
Example:
I have created a Product model and ProductPolicy classes. then I registered my ProductPolicy into AuthServiceProvider as below.
Product.php:
`class Product extends Model
{
use HasFactory;
}`
ProductPolicy.php
`namespace App\Policies;
use App\Models\Product;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Support\Facades\Log;
class ProductPolicy
{
use HandlesAuthorization;
}
`
AuthServiceProvider.php
`class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Product::class=>ProductPolicy::class,
];
....`
api.php ( Route )
`Route::controller(\App\Http\Controllers\AuthController::class)->group(function(){
Route::post('register', 'register');
Route::post('login', 'login');
});
Route::middleware('auth:api')->group( function () {
Route::resource('products', \App\Http\Controllers\ProductController::class);
});`
ProductController.php
`class ProductController extends BaseController
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
Now, when I consumed the below curl request, the ProductPolicy is not invoked. As you can see I use the Laravel Log just to make sure that my policy is invoked.
So, could you please help me to understand when the policy will be invoked? and if there is anyway to invoke it when any model action is happening ( searching, create, update etc .. ).
Beta Was this translation helpful? Give feedback.
All reactions