Different action for route if not logged in

Can anybody give me a hint how to do that best?

I would like to have one route which points to a different action if the user is not logged in.


user is logged in:

‘my-pattern’ => controller/action-a

user is not logged in:

‘my-pattern’ => controller/action-b

Thank you!

Put in a simple test to determine if the user is logged in, if yes, show one nav item, if not show the other:


if (!Yii::$app->user->isGuest)


	echo Html::a('Link name', ['controller/action-a'], ['class' => 'btn btn-primary']);




	echo Html::a('Link name', ['controller/action-b'], ['class' => 'btn btn-primary']);



[size="2"]I have not tested, but I think that should do it.[/size]

[size="2"]You will need to include:[/size]


use yii\helpers\Html;


[size="3"]at the top of the file.[/size]

Thank you!

But that will not work. If I have one URL for two different actions, how does urlManager know which is the correct route? So the logic must be in the urlManager rule.

I think a custom rule via yii\web\UrlRule is the only way to do it. I hoped there would be a faster/easier way.

Thank you anyway!


In the action for that route, you should redirect the user to the right route.