Search code examples
phplaravelyii2

check current route yii2


I just started migrating from laravel to yii2

In Laravel, I had a moment when I checked the current route, and if we are on this page, then we leave only the text, and if not, then we make this text a link

@if(Route::currentRouteName() === 'contact')
  <span class="contact-link active">contact</span>
@else
  <a href="{{ route('contact') }}" class="contact-link">contact</a>
@endif

Now I want to do exactly the same on yii2

With a regular link, everything is clear

<a href="<?= Url::to(['/contact']) ?>" class="contact-link">contact</a>

But how can you check the current page?

My controller

public function actionIndex()
    {
        return $this->render('contact');
    }

Solution

  • You can call getUniqueId() on currently active action:

    if (Yii::$app->controller->action->getUniqueId() === 'controller/contact') {
    

    or use $id property if you want "relative" ID of current action:

    if (Yii::$app->controller->action->id === 'contact') {