O
O
Orbb2018-04-03 14:13:28
Yii
Orbb, 2018-04-03 14:13:28

How to make a seo module for Yii2?

Greetings!
Now I have a small site where different controllers are responsible for different url zones.
The problem is this: before that, I wrote an information book, where all subcategories and categories were implemented in one place - one controller, one method - it was not difficult to implement the ability to change the title and description at the page base level.
Here the situation is more complicated - there are 6 controllers, 3 sections are unique, each controller has 7-15 actions. Accordingly, you will have to insert the same checks everywhere and duplicate the code - well, what the hell. I don’t know all the possibilities of Yii2, the option of moving this to the main layout is spinning in my head, because he is alone on the entire front, but I consider this option only as an extremely spare one.
Tell me where to go and how to implement this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-04-03
@webinar

Of course, you need to look at the structure, but:
You can write a helper and pull its method so as not to duplicate the code.
It is possible to write behavior and to connect in the controler.
You can write a component and attach it in the config. In the same place, hook on an event, for example, beforeAction and execute its method.

V
Vitaly Khomenko, 2018-04-03
@iiifx

... to the main layout, because he is alone on the entire front, but I consider this option only as an extremely spare one.

Why? Is it a problem to connect a component at a single desired point?
I am adding:
/**
 * Зарегистрировать все тэги метаданных для страницы с правила
 */
public function applySeoRule ()
{
    # Не применяем правила если отключен
    if ( ! $this->isEnabled() ) {
        return;
    }
    # Только если есть правило для SEO
    if ( $this->hasSeoRule() ) {
        $view = Yii::$app->controller ?
            Yii::$app->controller->getView() :
            Yii::$app->getView();
        $seoRule = $this->getSeoRule();
        # Параметры индексации
        if ( $seoRule->is_no_index ) {
            $view->registerMetaTag( [
                'name' => 'robots',
                'content' => 'noindex',
            ], 'robots' );
        }
        # Основные метаданные
        if ( $seoRule->title !== '' ) {
            $view->title = $seoRule->title;
        }
        if ( $seoRule->description !== '' ) {
            $view->registerMetaTag( [
                'name' => 'description',
                'content' => $seoRule->description,
            ], 'description' );
        }
        if ( $seoRule->keywords !== '' ) {
            $view->registerMetaTag( [
                'name' => 'keywords',
                'content' => $seoRule->keywords,
            ], 'keywords' );
        }
        # Метаданные OpenGraph
        $opengraphTags = [
            'opengraph_title' => 'og:title',
            'opengraph_site_name' => 'og:site_name',
            'opengraph_url' => 'og:url',
            'opengraph_description' => 'og:description',
        ];
        $registerOpengraph = false;
        foreach ( $opengraphTags as $attribute => $tagName ) {
            if ( isset( $seoRule->$attribute ) && $seoRule->$attribute !== '' ) {
                $view->registerMetaTag( [
                    'name' => $tagName,
                    'content' => $seoRule->$attribute,
                ], $tagName );
                $registerOpengraph = true;
            }
        }
        if ( $seoRule->opengraphImage ) {
            $view->registerMetaTag( [
                'name' => 'og:image',
                'content' => $seoRule->opengraphImage->getFileUrlWithDomain(),
            ], 'og:image' );
            $registerOpengraph = true;
        }
        if ( $registerOpengraph ) {
            $view->registerMetaTag( [
                'name' => 'og:locale',
                'content' => str_replace( '-', '_', Yii::$app->language ),
            ], 'og:locale' );
            $view->registerMetaTag( [
                'name' => 'og:type',
                'content' => 'website',
            ], 'og:type' );
        }
        # Twitter OpenGraph
        $twitterTags = [
            'twitter_url' => 'twitter:url',
            'twitter_title' => 'twitter:title',
            'twitter_description' => 'twitter:description',
            'twitter_site' => 'twitter:site',
            'twitter_creator' => 'twitter:creator',
        ];
        $registerTwitter = false;
        foreach ( $twitterTags as $attribute => $tagName ) {
            if ( isset( $seoRule->$attribute ) && $seoRule->$attribute !== '' ) {
                $view->registerMetaTag( [
                    'name' => $tagName,
                    'content' => $seoRule->$attribute,
                ], $tagName );
                $registerTwitter = true;
            }
        }
        if ( $seoRule->twitterImage ) {
            $view->registerMetaTag( [
                'name' => 'twitter:image',
                'content' => $seoRule->twitterImage->getFileUrlWithDomain(),
            ], 'twitter:image' );
            $registerTwitter = true;
        }
        if ( $registerTwitter ) {
            $view->registerMetaTag( [
                'name' => 'twitter:card',
                'content' => 'summary',
            ], 'twitter:card' );
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question