I’m running into a simple problem that I can’t seem to figure out (probably because I’m still a noob at all of this).
Anyway, I have a parent controller that I use to redirect users to confirm their emails. I’m extending all other controllers from it. However, in doing so, I’ve discovered that my “behaviors” no longer work.
How do I fix this?
Thanks in advance.
softark
(Softark)
January 28, 2018, 2:30pm
2
Do you mean the behaviors of the parent?
Probably you have to merge the parent’s behaviors in a child behaviors() method.
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors, [
...,
...
]);
}
Hmm, I tried this solution and it says "undefined class: behaviors".
To answer your question, I mean that the behaviors in my child classes do not work. Sorry for not making that clear.
softark
(Softark)
February 2, 2018, 11:56pm
4
Oh, I’m sorry. I forgot the parentheses.
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(), [
...,
...
]);
}
Some bug may be in the child’s behavior itself. I don’t believe the parent class interferes the child’s behavior.
Hmm, still not behaving properly. I am still able to access ‘new-today’ even when I’m not logged in.
class TodayController extends RedController
{
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(), [
'access' => [
'class' => AccessControl::className(),
'only' => ['new-today'],
'rules' => [
[
'actions' => ['new-today'],
'allow' => true,
'roles' => ['@']
]
]
]
]);
}
}
What am I missing here?
softark
(Softark)
February 7, 2018, 9:44am
6
What do you have in the parent’s behaviors?
Nothing. I don’t have anything implemented in the RedController.
I even tried adding
public function behaviors()
{
return [];
}
Still no luck.
softark
(Softark)
February 10, 2018, 4:49am
8
Hmm, I don’t know what’s wrong …
Would you be able to try it on a blank app and see if you can repeat the issue? Maybe this needs to be escalated.
I don’t know what else to check for or do.
softark
(Softark)
February 15, 2018, 12:52pm
10
I’ve just tried to reproduce the issue with one of my projects, and I couldn’t reproduce it. It works as expected.
BaseController
namespace app\controllers;
....
class BaseController extends Controller
{
public function behaviors()
{
return [
];
}
....
}
HealthController
namespace app\controllers;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
class HealthController extends BaseController
{
public function behaviors()
{
return ArrayHelper::merge(parent::behaviors(), [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
]);
/* --- this also works good
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
];
*/
}
public function actionIndex()
{
return $this->render('index');
}
}
I tried to access "/health/index" as a guest, but I could not and got redirected to the login page.
After logged in, I could successfully access "/health/index" page.
So I did a bit more exploring.
The reason why the behaviors aren’t working in my child controller is because I have a beforeAction in the parent controller that was not properly implemented.
I needed to change my "return true" line to "return parent::beforeAction($action)".
Now everything works as expected.
I did not think that beforeAction would effect behaviors at all, but lesson learned.
softark
(Softark)
February 17, 2018, 1:06pm
12
Glad you solved the issue. And thank you for sharing your experience. I also learned a lesson.