YII Framework Behavior behaviors Usage Example
- 2021-12-05 05:58:26
- OfStack
This article illustrates the YII framework behavior behaviors usage. Share it for your reference, as follows:
Document 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{
// In action Can be used to filter input
public function beforeAction($action) {
echo ' When calling action Front display <br/>';
return TRUE;// If the return value is false, Then action Will not run
}
// In action After that, it can be used to filter the output
public function afterAction($action, $result) {
return $result.' When calling action Back display <br/>';// Can be right action Output $result Filter, retun The contents of 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',// Invoke filter
],
];
}
public function actionFilter(){
return " Current action Display <br/>";// The returned content is submitted to the filter, and the afterAction Deal with
}
}
More readers interested in Yii can check the topics of this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Basic Tutorial of Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
I hope this article is helpful to PHP programming based on Yii framework.