Yii2 Two different way for breadcrumbs

176 views Asked by At

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.
2

There are 2 answers

3
Skatox On BEST ANSWER
  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];

0
WeSee On

This is a short breadcrum solution using match():

$this->params['breadcrumbs'][] = match($path) {
    'creation' => ['label' => 'Creation', 'url' => Url::to['creation'],
    'summary' => ['label' => 'Summary', 'url' => Url::to['summary'],
};

$path should either be set in the controller or determined by Yii::$app->request->getReferrer().

Please note, that this requires PHP8.