Answer the question
In order to leave comments, you need to log in
Yii: Generic SEO Meta Tag Output Component
I would like to make a universal tool for displaying SEO meta tags for any data model and wondered how best to implement it:
1. Through Behavior - I'm afraid that it won't work, because it doesn't interact with rendering. Those. this code unfortunately doesn't work
SeoBehavior.php
<[code>
…
public function beforeSave($event){
Yii::app()->clientScript->registerMetaTag('This is an example', 'description');
return parent::beforeSave($event);
}
...
It is also not clear in this approach how to create an html tag [TITLE]
.
2. through the module. But then it is not very obvious how to connect it. I would like to avoid a too centralized include (in the config, because different models may have different rules for compiling meta tags)) and an unnecessarily complex connection.
What would you like ideally?
Ideally, as I see the work of “tweaking” something like this: we connect it in the model, indicating which fields are responsible for which meta tags in the render. Then he does everything himself.
What are the ideas for implementation?
'seo' => array(
'class'=>'ext.seo.SeoBehavior',
'titleField'=>'title',
),
Answer the question
In order to leave comments, you need to log in
Now search engines do not pay attention to keywords and description meta tags. It makes no sense to put them on the page at all. The headline should be the headline, without any pretensions.
And in terms of implementation, why not do it like this? ..
//...
public function beforeSave($event){
Yii::app()->controller->setMetaData('title', 'title...');
Yii::app()->controller->setMetaData('keywords', 'keywords...');
}
//...
class Controller extends CController
{
private $_metaData = array();
public function setMetaData($name, $value)
{
$this->_metaData[$name] = $value;
}
public function beforeRender()
{
if(isset($this->_metaData['title']))
{
$this->pageTitle = $this->_metaData['title'];
unset($this->_metaData['title']);
}
foreach($this->_metaData as $name => $value)
Yii::app()->clientScript->registerMetaTag($name, $value);
}
}
I am making a module for setting SEO parameters for a specific model and route: github.com/nevmerzhitsky/yii2-seomodule.
You can't get away from the "complex connection" if the data of a specific model must be used to generate the meta tag. After all, you need to configure how to get data for description, keywords and title from the model.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question