How/Where should I set my page meta data? SEO Tips

I was wondering how should I set up my page meta (should i set it in my view or controller does this depend on complexity of seo)?

Can I have site wide meta for title, description and keywords. And then within my controller/views set the meta.

For example within a CRUD view say an individual article I have the below in the view, however if their a better way to do this.


$title = $model->title;

$description = substr($model->summary,0,155); 

$url = Yii::$app->request->url;

$keywords = "Some Default Key Words";


if (!empty($model->seo_title)) {

    $title = $model->seo_title;

}


if (!empty($model->meta_desc)) {

    $description = $model->meta_desc;

}


if (!empty($model->keywords)) {

    $keywords = $model->keywords;

}


$this->title = Yii::t('app', '' . $title . '');


$this->registerMetaTag([

    'name' => 'description',

    'content' => '' . $description . ''

]);

$this->registerMetaTag([

    'name' => 'keywords',

    'content' => '' . $keywords . ''

]);