Controller default action

This question has already posted a lot of time ago: $defaultAction is not working

I post it again because of I’m not understanding at all: Application Structure: Controllers | The Definitive Guide to Yii 2.0 | Yii PHP Framework

If I understand it, if I’d like to set the login as the default action, I only should have to set the $defaultAction to ‘login’ and, when I call the website as for instance http://localhost it should be redirected or at least to display the login page but actually the index is displayed.

In this case, Do I have to forget the defaultAction attribute and instead to set a site/login redirect in the index action?

Hi.
Have a look on Routing and URL Creation: Default Route
It should do the job.

I think this will do (if not already logged in)
https://www.yiiframework.com/doc/guide/2.0/en/structure-filters#access-control
(remove the “only” line)