1

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.
WorraccCdP
  • 39
  • 6

2 Answers2

2
  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];

Skatox
  • 4,237
  • 12
  • 42
  • 47
  • I LOVE YOU!!<3<3<3 I used the switch and it works great – WorraccCdP May 02 '22 at 17:51
  • One question, I have a link which at the end has the alert id, how do I indicate the number on the switch case? http: // localhost / web / reporting / update / %% where the id of the report goes in place of the percentage. – WorraccCdP May 03 '22 at 10:24
  • 1
    Dependends on your URLManager configuration, you need to see how you can get the variable from the url, or I guess it's just $_GET['id'], but this is a new question. – Skatox May 03 '22 at 17:49
2

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.

WeSee
  • 3,158
  • 2
  • 30
  • 58