LandingController.php 1.68 KB
<?php
    namespace frontend\controllers;

    use artbox\core\components\SeoComponent;
    use artbox\core\models\Language;
    use artbox\core\models\Alias;
    use artbox\core\models\PageLang;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\base\ViewNotFoundException;
    use Yii;

    /**
     * Class LandingController
     *
     * @package frontend\controllers
     */
    class LandingController extends Controller
    {
        public function actionView($id)
        {
            // // Sets meta-tags on page, previously added through the admin panel -> "SEO page || route set as json with index ¯\_(ツ)_/¯ -> {"0":"komplekty"}
            $seo = \Yii::$app->get('seo');
            $language = Language::getCurrent();

            $alias = Alias::findRoute([ $id ], $language);
            if( empty($alias) ) throw new NotFoundHttpException();
            $seo->setAlias($alias);

            $model = $this->findModel($alias->id, $language->id);

            return $this->renderPartial(
                'view',
                [
                    'model' => $model,
                ]
            );


        }

        protected function findModel($id, $lang)
        {
            /**
             * @var Page $model
             */
            $model = PageLang::find()
                         ->where(
                             [
                                'alias_id' => $id,
                                'language_id' => $lang
                             ]
                         )
                         ->one();

            if ( empty($model) ) throw new NotFoundHttpException('Model not found');

            return $model;
        }

    }