Reputation: 13544
In my yii2 controller's action, the page title may be set as follows:
$this->view->title .= ' | Some title';
The problem here, it is not seem to have a default title for the whole of the application. i.e My site name | the page title.
The only way that I, currently, know to work is to make another controller class inherited from the controller
class and the define or set $this->view->title
define it in the construction method, then inherit all of my controllers from this class like the following:
namespace common\controllers
use Yii;
use yii\web\Controller;
class MainController extends Controller
{
function __construct()
{
$this->view->title = 'My site name ';
}
}
Then in any controller I should extend from MainController
.
The question is:
Is there any configuration way or bootstrap way to set a default value for the page title, then I can concatenate another value to it through the action?
Upvotes: 1
Views: 4623
Reputation: 25312
You could simply handle this in your layout :
<title>My site name | <?= Html::encode($this->title) ?></title>
And after in your view, you just have to set the title like this :
$this->title = 'Some title';
Or you could also set the application name in your config
$config = [
'name' => 'My site name',
...
];
And use it in your layout :
<title><?= Yii::$app->name ?> | <?= Html::encode($this->title) ?></title>
Upvotes: 6