プロパティの変更ロック
Livewireのプロパティは、wire:model
のようなユーティリティを使用して、フロントエンドとバックエンドの両方で自由に修正できます。モデルIDのようなプロパティがフロントエンドで変更されるのを防ぎたい場合は、Livewireの#[Locked]
属性を使用できます。Livewire properties are able to be modified freely on both the frontend and backend using utilities like wire:model
. If you want to prevent a property — like a model ID — from being modified on the frontend, you can use Livewire's #[Locked]
attribute.
基本的な使用法Basic usage
以下は、Post
モデルのIDを$id
という名前のパブリックプロパティとして格納するShowPost
コンポーネントです。このプロパティが好奇心旺盛なユーザーや悪意のあるユーザーによって変更されるのを防ぐために、#[Locked]
属性をプロパティに追加できます。Below is a ShowPost
component that stores a Post
model's ID as a public property named $id
. To keep this property from being modified by a curious or malicious user, you can add the #[Locked]
attribute to the property:
Warning! 属性クラスをインポートしてください すべての属性クラスをインポートしてください。たとえば、以下の
#[Locked]
属性は、次のインポートが必要です:use Livewire\Attributes\Locked;
。[!warning] Make sure you import attribute classes Make sure you import any attribute classes. For example, the below#[Locked]
attribute requires the following importuse Livewire\Attributes\Locked;
.
use Livewire\Attributes\Locked;
use Livewire\Component;
class ShowPost extends Component
{
#[Locked] // [tl! highlight]
public $id;
public function mount($postId)
{
$this->id = $postId;
}
// ...
}
#[Locked]
属性を追加することで、$id
プロパティが改ざんされることはありません。By adding the #[Locked]
attribute, you are ensured that the $id
property will never be tampered with.
Tip: モデルプロパティはデフォルトで安全です モデルのIDだけでなく、Eloquentモデルをパブリックプロパティに格納する場合、Livewireは、明示的に
#[Locked]
属性をプロパティに追加しなくても、IDが改ざんされないようにします。ほとんどの場合、これは#[Locked]
を使用するよりも優れた方法です。[!tip] Model properties are secure by default If you store an Eloquent model in a public property instead of just the model's ID, Livewire will ensure the ID isn't tampered with, without you needing to explicitly add the#[Locked]
attribute to the property. For most cases, this is a better approach than using#[Locked]
:class ShowPost extends Component { public Post $post; // [tl! highlight] public function mount($postId) { $this->post = Post::find($postId); } // ... }
なぜprotectedプロパティを使用しないのですか?Why not use protected properties?
機密データにprotectedプロパティを使用しないのはなぜか?と疑問に思うかもしれません。You might ask yourself: why not just use protected properties for sensitive data?
Livewireは、ネットワークリクエスト間でパブリックプロパティのみを保持することを覚えておいてください。静的なハードコードされたデータの場合、protectedプロパティが適しています。ただし、実行時に格納されるデータの場合、データが適切に保持されるように、パブリックプロパティを使用する必要があります。Remember, Livewire only persists public properties between network requests. For static, hard-coded data, protected properties are suitable. However, for data that is stored at runtime, you must use a public property to ensure that the data is persisted properly.
Livewireはこれを自動的に行えないのですか?Can't Livewire do this automatically?
理想的な世界では、Livewireはデフォルトでプロパティをロックし、wire:model
がそのプロパティで使用されている場合にのみ変更を許可します。In a perfect world, Livewire would lock properties by default, and only allow modifications when wire:model
is used on that property.
残念ながら、そのためには、LivewireがすべてのBladeテンプレートを解析して、プロパティがwire:model
または同様のAPIによって変更されているかどうかを理解する必要があります。Unfortunately, that would require Livewire to parse all of your Blade templates to understand if a property is modified by wire:model
or a similar API.
それは技術的およびパフォーマンスのオーバーヘッドを追加するだけでなく、Alpineやその他のカスタムJavaScriptのようなものでプロパティが変更されたかどうかを検出することは不可能です。Not only would that add technical and performance overhead, it would be impossible to detect if a property is mutated by something like Alpine or any other custom JavaScript.
したがって、Livewireは引き続きパブリックプロパティをデフォルトで自由に変更可能にし、必要に応じてそれらをロックするためのツールを開発者に提供します。Therefore, Livewire will continue to make public properties freely mutable by default and give developers the tools to lock them as needed.