Search code examples
yii2yii2-basic-appgii

Yii2 Two different way for breadcrumbs


I have a gii generated view.php page, which can be reached from two different paths, but the breadcrumbs is the same. Anyone know how to fix?

Let me explain: I have the view.php view which shows the summary of the report made and can be reached from two paths:

  • when I create a new report and therefore the path should be HOME / CREATION / VIEW;
  • and also from a section that shows the user the summary of the reports she sent and therefore the breadcrumbs should be HOME / SUMMARY / VIEW.

Solution

    1. You have 2 options to know where it came from:
    • Send a query variable on each link or in the redirection to manually build the breadcumb. Like: $value = Yii::$app->request->getQueryParam('breadcumb')

    • Other option is to get the referrer url. And base on the value you can pass it to the switch. You can get referrer's url using: $value = Yii::$app->request->getReferrer().

    1. Then do a switch to build the link:

      switch(value) {
        case 'creation':
         $label = 'CREATION';
         $url = 'url_of_creation';
         break;
        case 'summary':
         $label = 'SUMMARY';
         $url = 'url_of_summary'; 
         break;
      }
      
    2. Then just do something like this:

    $this->params['breadcrumbs'][] = ['label' => $label, 'url' => $url];