UrlRule.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 2017/3/6
  6. * Time: 下午9:38
  7. */
  8. namespace common\modules\urlrule\models;
  9. use common\behaviors\CacheInvalidateBehavior;
  10. use common\behaviors\PositionBehavior;
  11. use yii\db\ActiveRecord;
  12. use Yii;
  13. /**
  14. * This is the model class for table "{{%url_rule}}".
  15. *
  16. * @property integer $id
  17. * @property string $name
  18. * @property string $pattern
  19. * @property string $host
  20. * @property string $route
  21. * @property string $defaults
  22. * @property string $suffix
  23. * @property string $verb
  24. * @property integer $mode
  25. * @property integer $encodeParams
  26. * @property integer $status
  27. */
  28. class UrlRule extends ActiveRecord
  29. {
  30. /**
  31. * @inheritdoc
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%url_rule}}';
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['pattern', 'route'], 'required'],
  44. [['mode', 'encodeParams', 'status'], 'integer'],
  45. [['name'], 'string', 'max' => 50],
  46. [['defaults', 'verb', 'name', 'host'], 'default'],
  47. [['pattern', 'host', 'route', 'defaults', 'suffix', 'verb'], 'string', 'max' => 255],
  48. ];
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'name' => 'Name',
  58. 'pattern' => 'Pattern',
  59. 'host' => 'Host',
  60. 'route' => 'Route',
  61. 'defaults' => 'Defaults',
  62. 'suffix' => 'Suffix',
  63. 'verb' => 'Verb',
  64. 'mode' => 'Mode',
  65. 'encodeParams' => 'Encode Params',
  66. 'status' => 'Status',
  67. ];
  68. }
  69. public function behaviors()
  70. {
  71. return [
  72. [
  73. 'class' => CacheInvalidateBehavior::className(),
  74. 'keys' => ['openRules']
  75. ],
  76. [
  77. 'class' => PositionBehavior::className(),
  78. 'positionAttribute' => 'sort'
  79. ]
  80. ];
  81. }
  82. public function beforeSave($insert)
  83. {
  84. if(parent::beforeSave($insert)== false) {
  85. return false;
  86. }
  87. if($insert == true) {
  88. $this->mode = 0;//0,1,2 分别代表适用于创建和解析.只能创建,只能解析
  89. $this->encodeParams = 1;
  90. $this->status = 1;
  91. }
  92. return true;
  93. }
  94. public static function findOpenRules()
  95. {
  96. $rules = Yii::$app->cache->get('openRules');
  97. if ($rules === false) {
  98. $rules = static::find()->where(['status' => 1])->orderBy('sort asc')->all();
  99. Yii::$app->cache->set('openRules', $rules);
  100. }
  101. return $rules;
  102. }
  103. }