Creating filters
A filter is a class that can run before/after an action is executed. It can be used to modify execution context or decorate output. In our example, we'll implement a simple access filter that will allow the user to see private content only after accepting the User agreement.
Getting ready
Create a new yii2-app-basic
application using the composer, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
How to do it…
- Create the agreement form model:
<?php namespace app\models; use yii\base\Model; class AgreementForm extends Model { public $accept; public function rules() { return [ ['accept', 'required'], ['accept', 'compare', 'compareValue' => 1, 'message' => 'You must agree the rules.'], ...
Get Yii2 Application Development Cookbook - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.