Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.