Trying to create complete path with breadcrumbs

In the application that i’m improving, i would like to implement breadcrumbs so that the user can always go back even more than one page; i used some variables (with session) that i set every time the user opens a view from the previous one and it seems work well.
Some views, however, are in common, so it happens that the path also includes what it don’t need. I would need to reset the variables when the user makes the choice from the menu (navbar), so that the breadcrumbs are correct, but I can’t intercept these commands; i tried from SiteController (in beforeActions()), but the navbar commands don’t go through there; other solution may be duplicate views, but i don’t like.
Any suggestion ?

I also read and try these articles to see if they could help: Breadcrump widget and Auto breadcrumbs but i can’t get them to work; one go in error in view for “$this->widget” and the second for “getTitlePage()”.
Maybe because I’m using Yii2 ?

Thanks

I agree duplicating views is not a good idea. The alternative to duplicating the views is duplicating the controller action. For e.g. if you have the actionIndex() in your controller, you can create another actionJindex() that simply redirects to actionIndiex. This public function will only require one line

use yii\helpers\Url;
    public function actionJindex()
    {
        // Do optional processing here if required
        return $this->redirect([Url::to(['index'])]);
    }

You can also now manipulate the breadcrumbs in Jindex action if you like to reset it. I use this technique when I need to perform some additional validations, setting of sessions, etc. that happens from certain places in the application but not always. Instead of recreating a different view or even copy/pasting a new controller action, this a clean way to doing the extra steps you need at times for the same logical action.

Yii Already comes with Breadcrumb widget and it works fine. What problem do you face?

@snathan
It’s a great idea !!!
I try as soon as possible.
Only one question: how i call them from navbar ? ‘Jindex’ instead of ‘index’, it’s correct ?

@evstevemd
I found these articles and i try to use but i don’t spend much time; i just only see what is result, but returns some error: don’t found “widget” in view when i wrote “$this->widget( …” and dont’ found function getTitlePage() on main.php; i think i make some mistake or missing some header.

Thanks

Correct. Perhaps lowercase “j”. You can first try it out by typing it in the browser address bar.

Yes, sure, with lowercase … and maybe with a name that also indicates the function.
Now i’m tired, but i try as soon as possible.
Thanks

Ok, it works very well.
I have to add some condition in view to reset the last variable saved, because, when the user return back, one more breadcrump of previous view is showed.
On controller:

    /**
     * Lists all Productstrace models.
     * for index calls from other index
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ProductstraceSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    /**
     * Lists all Productstrace models.
     * for index calls from menu
     * @return mixed
     */
    public function actionIndexmenu()
    {
        Yii::$app->session['urlPrevious1'] = 'productstracemenu';       
        Yii::$app->session['urlPrevious2'] = 'none';
        Yii::$app->session['urlPrevious3'] = 'none';

        $searchModel = new ProductstraceSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

On view

if ((Yii::$app->session['urlPrevious1'] == 'dailystatistics') OR
	 (Yii::$app->session['urlPrevious1'] == 'scrapproduct'))
{
	Yii::$app->session['urlPrevious2'] = 'productstrace';
}
else if (Yii::$app->session['urlPrevious1'] == 'productstracemenu')) // This i have to added
{
	Yii::$app->session['urlPrevious2'] = 'none';
	Yii::$app->session['urlPrevious3'] = 'none';
}

if (Yii::$app->session['urlPrevious2'] == 'componentstrace')
{
	Yii::$app->session['urlPrevious3'] = 'none';
}

if (Yii::$app->session['urlPrevious1'] == 'dailystatistics')
{
    $this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Statistics analisys'), 'url' =>
        ['dailystatistics/index', 'DailystatisticsSearch[date]' => Yii::$app->getRequest()->getQueryParam(date('Y-m-d'))]];
}

// ... and so on

Thanks again.

Glad it worked. I noticed you are duplicating the code in the controller. Instead you can simple redirect at the end of IndexMenu (after the 1st 3 lines) to Index. It’s not a big deal, but keeps in cleaner and in those instances where you need to change Index (say you want to add a where condition to the query) you don’t have to change IndexMenu as well.

I’m starting to doubt that I have misunderstood; I realized you meant to duplicate the “index” action, so I can put in the initializations I need.
The “index.php” view is the same for both actions, as well as for models and all the rest; what changes is only the name of the action and its function.
Maybe you mean, instead of brutally copying the lines that open index, to call the actionIndex () function, like this:

public function actionIndexmenu()
    {
        Yii::$app->session['urlPrevious1'] = 'productstracemenu';       
        Yii::$app->session['urlPrevious2'] = 'none';
        Yii::$app->session['urlPrevious3'] = 'none';

        $this->actionIndex();
       
    }

Yes, now i understand: in this way it’s better.
Thanks

Am trying to understand what you are facing. Can you show some image or code?

Instead of:

You have to do a redirect
return $this->redirect([Url::to(['index'])]);

When you do a redirect it pretty much as if you called index, as the URL also changes to index, except you did not (call index) and also that you were able to include additional processing.

What you have will work, but by redirecting you avoid copy/paste of the same code.

@snathan
I think to understand.
Thanks

@evstevemd
Code is like as i reported earlier.
Result:

I get hard time understanding.
How can someone come to Measures? AFAIU, it should be via very specific like, for example from either sidebar link or Index page or some other predictable source. In such case, include page code in query part of URL and use that to determine “back”

But what are you trying to accomplish? May be there are better ways!

From statistics user can choice a main component, good or scrap, and see all components on which it was assembled, on what stations and their measures; and now he can return back where he wants.
On menu user can also select measures directly, but he see all measures, of all stations and components.
It’s possible that exist a better way, but at the moment i have this way, with this database and these tables, and i have to improve application.
The possibility to return back and select another component or another main component before don’t exist: the user can only begin again.