Skip to content

Instantly share code, notes, and snippets.

@fedek6
Created December 3, 2018 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedek6/c15b6ef07c134c9ab1cd60535a365c33 to your computer and use it in GitHub Desktop.
Save fedek6/c15b6ef07c134c9ab1cd60535a365c33 to your computer and use it in GitHub Desktop.
Prepend application name to site title in Yii2
<?php
/**
* Extension of base controller.
* Add additional controller functions and vars here.
*/
namespace app\components;
use Yii;
class Controller extends \yii\web\Controller
{
/**
* {@inheritdoc}
*/
public function init()
{
// Prepend application name to site title
$this->view->on('afterRender', function($event)
{
if(!empty(Yii::$app->view->title))
{
Yii::$app->view->title = Yii::$app->name . Yii::$app->params['titleSeparator'] . Yii::$app->view->title;
}
else
{
Yii::$app->view->title = Yii::$app->name;
}
});
}
}
<?php
/* @var $this yii\web\View */
$this->title = 'test';
?>
<?php
return [
'adminEmail' => 'admin@example.com',
'titleSeparator' => ' – ',
];
<?php
namespace app\controllers;
use Yii;
use app\components\Controller;
class SiteController extends Controller
{
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment