# Yii2 控制器里的 beforeAction 和 behaviors 里的 beforeAction 重复的问题

**URL:** <https://www.shiqidu.com/t/topic/627>\
**Category:** 开发调优\
**Tags:** Yii\
**Created:** [August 14, 2019, 8:05am UTC](https://www.shiqidu.com/t/topic/627 "2019-08-14T08:05:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
**Post date:** [August 14, 2019, 8:05am UTC](https://www.shiqidu.com/t/topic/627/1 "2019-08-14T08:05:35Z")

</div>

控制器 `BaseController` 的 behaviors 定义如下

```php
public function behaviors()
{
	return [
		'visit' => [
			'class' => VisitBehavior::className(),
		]
	];
}

```

`VisitBehavior` 里有一个 `beforeAction` 方法，用来做一些权限认证的事情，控制器 `BaseController` 里也有一个 `beforeAction` 方法，结果发现 `VisitBehavior` 里的 `beforeAction` 方法没有执行，很神奇，打印东西也不执行，后来发现，是因为 `BaseController` 的 `beforeAction` 方法少些了一行代码

```php
if (!parent::beforeAction($action)) {
	return false;
}

```

记录一下踩过的坑。
