composer install zacksleo/yii2-recently-viewed-behavior
use yii\web\Controller;
use zacksleo\yii2\behaviors\RecentlyViewedBehavior;
class DefaultController extends Controller
{
public function behaviors()
{
return [
'recentlyViewed' => [
'class' => RecentlyViewedBehavior::className(),
'limit' => 5, // Limit the number of recently viewed items stored. 0 = no limit.
],
];
}
public function actionView($id)
{
// set recently models
$model = $this->findModel($id);
$this->setRecentlyViewed(get_class($model), $id);
// get recently models
$this->getRecentlyViewed(get_class($model));
}