Commit 8596ef4b015ae35c3d934c285c069c3329fa21a1
1 parent
f3b6b9b1
-Seo component ready
Showing
3 changed files
with
36 additions
and
9 deletions
Show diff stats
common/config/main.php
1 | <?php | 1 | <?php |
2 | + use artbox\core\components\SeoComponent; | ||
3 | + | ||
2 | return [ | 4 | return [ |
3 | 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', | 5 | 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', |
4 | 'components' => [ | 6 | 'components' => [ |
@@ -17,5 +19,8 @@ | @@ -17,5 +19,8 @@ | ||
17 | 'class' => 'yii2tech\filedb\Connection', | 19 | 'class' => 'yii2tech\filedb\Connection', |
18 | 'path' => '@common/config', | 20 | 'path' => '@common/config', |
19 | ], | 21 | ], |
22 | + 'seo' => [ | ||
23 | + 'class' => SeoComponent::className(), | ||
24 | + ], | ||
20 | ], | 25 | ], |
21 | ]; | 26 | ]; |
frontend/controllers/PageController.php
1 | <?php | 1 | <?php |
2 | namespace frontend\controllers; | 2 | namespace frontend\controllers; |
3 | - | 3 | + |
4 | + use artbox\core\components\SeoComponent; | ||
4 | use artbox\core\models\Page; | 5 | use artbox\core\models\Page; |
5 | use yii\helpers\Json; | 6 | use yii\helpers\Json; |
6 | use yii\helpers\Url; | 7 | use yii\helpers\Url; |
7 | use yii\web\Controller; | 8 | use yii\web\Controller; |
8 | use yii\web\NotFoundHttpException; | 9 | use yii\web\NotFoundHttpException; |
9 | - | 10 | + use Yii; |
11 | + | ||
10 | /** | 12 | /** |
11 | * Class PageController | 13 | * Class PageController |
12 | * | 14 | * |
@@ -17,9 +19,15 @@ | @@ -17,9 +19,15 @@ | ||
17 | public function actionView($id) | 19 | public function actionView($id) |
18 | { | 20 | { |
19 | $model = $this->findModel($id); | 21 | $model = $this->findModel($id); |
20 | - | 22 | + |
23 | + /** | ||
24 | + * @var SeoComponent $seo | ||
25 | + */ | ||
26 | + $seo = Yii::$app->get('seo'); | ||
27 | + $seo->setModel($model->lang); | ||
28 | + | ||
21 | var_dump(Url::to(Json::decode($model->lang->alias->route))); | 29 | var_dump(Url::to(Json::decode($model->lang->alias->route))); |
22 | - | 30 | + |
23 | return $this->render( | 31 | return $this->render( |
24 | 'view', | 32 | 'view', |
25 | [ | 33 | [ |
@@ -27,12 +35,25 @@ | @@ -27,12 +35,25 @@ | ||
27 | ] | 35 | ] |
28 | ); | 36 | ); |
29 | } | 37 | } |
30 | - | 38 | + |
31 | protected function findModel($id) | 39 | protected function findModel($id) |
32 | { | 40 | { |
33 | - $model = Page::findOne($id); | ||
34 | - | 41 | + /** |
42 | + * @var Page $model | ||
43 | + */ | ||
44 | + $model = Page::find() | ||
45 | + ->where( | ||
46 | + [ | ||
47 | + 'id' => $id, | ||
48 | + ] | ||
49 | + ) | ||
50 | + ->with('lang') | ||
51 | + ->one(); | ||
52 | + | ||
35 | if (!empty($model)) { | 53 | if (!empty($model)) { |
54 | + if ($model->lang->alias_id !== Yii::$app->seo->aliasId) { | ||
55 | + throw new NotFoundHttpException('Wrong language'); | ||
56 | + } | ||
36 | return $model; | 57 | return $model; |
37 | } else { | 58 | } else { |
38 | throw new NotFoundHttpException('Model not found'); | 59 | throw new NotFoundHttpException('Model not found'); |
frontend/views/page/view.php
1 | <?php | 1 | <?php |
2 | + use artbox\core\models\Language; | ||
2 | use artbox\core\models\Page; | 3 | use artbox\core\models\Page; |
3 | use yii\helpers\Url; | 4 | use yii\helpers\Url; |
4 | use yii\web\View; | 5 | use yii\web\View; |
@@ -7,7 +8,6 @@ | @@ -7,7 +8,6 @@ | ||
7 | * @var View $this | 8 | * @var View $this |
8 | * @var Page $model | 9 | * @var Page $model |
9 | */ | 10 | */ |
10 | - | ||
11 | ?> | 11 | ?> |
12 | 12 | ||
13 | <h1> | 13 | <h1> |
@@ -16,7 +16,8 @@ | @@ -16,7 +16,8 @@ | ||
16 | <?= Url::to( | 16 | <?= Url::to( |
17 | [ | 17 | [ |
18 | 'page/view', | 18 | 'page/view', |
19 | - 'id' => 10, | 19 | + 'id' => $model->id, |
20 | + 'lang' => Language::getCurrent()->id, | ||
20 | ] | 21 | ] |
21 | ) ?> | 22 | ) ?> |
22 | </h1> | 23 | </h1> |
23 | \ No newline at end of file | 24 | \ No newline at end of file |