Laravel 10.xのartisanの「make:policy」コマンドを解説します。
Description
新しいポリシークラスを作成します。
Usage
php artisan make:policy [-f|--force] [-m|--model [MODEL]] [-g|--guard [GUARD]] [--] <name>
Arguments
引数 | 必須 | 設営 |
---|---|---|
name | 引数 | ポリシークラス名 |
Options
オプション | 省略形 | 必須 | 説明 |
---|---|---|---|
force | f | - | ポリシーがすでに存在する場合でもクラスを作成 |
model | m | - | ポリシーが適用されるモデル |
guard | g | - | ポリシーが依拠するガード |
Examples
no options
php artisan make:policy FooPolicy
INFO Policy [app/Policies/FooPolicy.php] created successfully.
<?php
namespace App\Policies;
use App\Models\User;
class FooPolicy
{
/**
* Create a new policy instance.
*/
public function __construct()
{
//
}
}
model
php artisan make:policy --model=User FooPolicy
INFO Policy [app/Policies/FooPolicy.php] created successfully.
<?php
namespace App\Policies;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class FooPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, User $model): bool
{
//
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, User $model): bool
{
//
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, User $model): bool
{
//
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, User $model): bool
{
//
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, User $model): bool
{
//
}
}
Stub
Option | Stub file name |
---|---|
- | policy.stub |
model | policy.plain.stub |
Source
10.x
framework/src/Illuminate/Foundation/Console/PolicyMakeCommand.php at 10.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.
11.x
framework/src/Illuminate/Foundation/Console/PolicyMakeCommand.php at 11.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.
コメント