Remembering Sorting (Filters)

When someone sorts on my grid view for example, I want to remember how they sorted.

This is what I have so far, and I know its WRONG wrong wrong, so wrong. But it works. Before I go implementing this in all my models, can someone suggest a better way?


        if(Yii::$app->session->get('sort') != '' && Yii::$app->request->get('sort') == '')

        {

            //if minus is first char then make it sort_desc and remove the minus

            if(strstr(Yii::$app->session->get('sort'), '-'))

            {

                $sort_name = str_replace('-', '', Yii::$app->session->get('sort'));

                $sort_direction = SORT_DESC;

            }

            else

            {

                $sort_name = Yii::$app->session->get('sort');

                $sort_direction = SORT_ASC;

            }

            $default_sort = [

                $sort_name => $sort_direction

            ];

        }





        $dataProvider = new ActiveDataProvider([

            'query' => $query,

            'pagination' => [

                'pageSize' => 10,

            ],

            'sort' => [

                'defaultOrder' => $default_sort

            ]

        ]);