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 | 1 | <?php |
2 | + use artbox\core\components\SeoComponent; | |
3 | + | |
2 | 4 | return [ |
3 | 5 | 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', |
4 | 6 | 'components' => [ |
... | ... | @@ -17,5 +19,8 @@ |
17 | 19 | 'class' => 'yii2tech\filedb\Connection', |
18 | 20 | 'path' => '@common/config', |
19 | 21 | ], |
22 | + 'seo' => [ | |
23 | + 'class' => SeoComponent::className(), | |
24 | + ], | |
20 | 25 | ], |
21 | 26 | ]; | ... | ... |
frontend/controllers/PageController.php
1 | 1 | <?php |
2 | 2 | namespace frontend\controllers; |
3 | - | |
3 | + | |
4 | + use artbox\core\components\SeoComponent; | |
4 | 5 | use artbox\core\models\Page; |
5 | 6 | use yii\helpers\Json; |
6 | 7 | use yii\helpers\Url; |
7 | 8 | use yii\web\Controller; |
8 | 9 | use yii\web\NotFoundHttpException; |
9 | - | |
10 | + use Yii; | |
11 | + | |
10 | 12 | /** |
11 | 13 | * Class PageController |
12 | 14 | * |
... | ... | @@ -17,9 +19,15 @@ |
17 | 19 | public function actionView($id) |
18 | 20 | { |
19 | 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 | 29 | var_dump(Url::to(Json::decode($model->lang->alias->route))); |
22 | - | |
30 | + | |
23 | 31 | return $this->render( |
24 | 32 | 'view', |
25 | 33 | [ |
... | ... | @@ -27,12 +35,25 @@ |
27 | 35 | ] |
28 | 36 | ); |
29 | 37 | } |
30 | - | |
38 | + | |
31 | 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 | 53 | if (!empty($model)) { |
54 | + if ($model->lang->alias_id !== Yii::$app->seo->aliasId) { | |
55 | + throw new NotFoundHttpException('Wrong language'); | |
56 | + } | |
36 | 57 | return $model; |
37 | 58 | } else { |
38 | 59 | throw new NotFoundHttpException('Model not found'); | ... | ... |
frontend/views/page/view.php
1 | 1 | <?php |
2 | + use artbox\core\models\Language; | |
2 | 3 | use artbox\core\models\Page; |
3 | 4 | use yii\helpers\Url; |
4 | 5 | use yii\web\View; |
... | ... | @@ -7,7 +8,6 @@ |
7 | 8 | * @var View $this |
8 | 9 | * @var Page $model |
9 | 10 | */ |
10 | - | |
11 | 11 | ?> |
12 | 12 | |
13 | 13 | <h1> |
... | ... | @@ -16,7 +16,8 @@ |
16 | 16 | <?= Url::to( |
17 | 17 | [ |
18 | 18 | 'page/view', |
19 | - 'id' => 10, | |
19 | + 'id' => $model->id, | |
20 | + 'lang' => Language::getCurrent()->id, | |
20 | 21 | ] |
21 | 22 | ) ?> |
22 | 23 | </h1> |
23 | 24 | \ No newline at end of file | ... | ... |