Usage example of behavior behaviors of Yii framework

Time:2020-1-11

This article gives an example of the usage of the behavior behaviors of the Yii framework. To share with you for your reference, as follows:

File frontend / LIBS / filtertest.php

<?php
/**
 * Created by PhpStorm.
 * Date: 2016/5/27
 * Time: 14:16
 */
namespace frontend\libs;
use Yii;
use yii\base\Action;
use yii\base\ActionFilter;
class FilterTest extends ActionFilter{
  //Run before action to filter input
  public function beforeAction($action) {
    Echo 'show < br / >' before calling action;
    Return true; // if the return value is false, the action will not run
  }
  //Run after action to filter output
  public function afterAction($action, $result) {
    Return $result. 'display < br / >' after calling action; // you can filter the $result output from action, and the content of return will be displayed directly
  }
}

frontend/controllers/TestbehaviorController.php

<?php
/**
 * Created by PhpStorm.
 * Date: 2016/5/27
 * Time: 14:19
 */
 namespace frontend\controllers;
 use yii\web\controller;
class TestbehaviorController extends Controller{
  public function behaviors(){
    return [
      'test'=>[
       'class' = >' frontend \ LIBS \ filtertest ', // call filter
      ],
    ];
  }
  public function actionFilter(){
    Return "the current action displays < br / >"; // the returned content will be submitted to the filter and processed by afteraction
  }
}

For more information about the content of Yii, please refer to the following topics: introduction to Yii framework and summary of common skills, summary of PHP excellent development framework, basic course of smart template introduction, introduction to PHP object-oriented programming, summary of PHP string usage, introduction to PHP + MySQL database operation and PHP common database operation Skills summary

I hope that this article will be helpful for you to Design PHP program based on Yii framework.