I have implemented page titles and all pages are showing the titles neatly. Now I also want the website name to be displayed. I can code it like
<title><?php echo Yii::app()->name . $this->pageTitle ?></title>
But for those pages whose titles are not set (i.e they are set by default by CController
) sitename will get repeated.
I want to simply override the setPageTitle
method of my controller to prepend sitename. How to do so ?
You don't have to add $pageTitle
to the controller it is already a variable in the Controller
class, so as long as your controllers extend Controller
you should be fine. You can then set the page title anywhere you want. You can change it for the entire controller, or for an individual action or even on a view.
class MyController extends Controller {
public function actionAdmin() {
$this->pageTitle = 'I got set by action'; //only for this action
}
}
Or in a view
<?php
$this->pageTitle = 'I got set by the view'; //anytime this view gets called
?>
<h1>View File</h1>
If you want the site name always at the end of the title simply modify your main layout:
<title><?php echo CHtml::encode($this->pageTitle); ?> <?php echo Yii::app()->name; ?></title>
This is what i solved the problem with attempt to override the method setPageTitle method of CController
class MyController extends Controller
{
public function setPageTitle($value){
$this->pageTitle = Yii::app()->name ." >> ". $value ;
}
}
Now the value of this would be set in the layout without the app name i.e
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
and in the view remove the app name.
$this->pageTitle = Yii::app()->name . ' >> Custom Title' ;//remove this to show instead
$this->pageTitle = 'Custom Title' ;//Plain title
Now the output will be
My App Name >> Custom Title
If some pages already have concatenated the app name in the view for those controllers you need to override the setPageTitle in that controllers only.But a better approach is to follow a common thing throughout hence override the setPageTitle in base controller.
If you want something more 'automatic', for example:
You can make a filter like this: (also works with multi-language!!!)
1- Create a file PageTitleFilter.php in protected/components/
class PageTitleFilter extends CFilter {
public $controller;
protected function preFilter($filterChain) {
// logic being applied before the action is executed
$this->controller->pageTitle = Yii::t('app', Yii::app()->controller->action->id) . ' ' . Yii::t('app', Yii::app()->controller->id);
return true; // false if the action should not be executed
}
protected function postFilter($filterChain) {
// logic being applied after the action is executed
}
}
2- In your controller:
class MyController extends Controller {
public function filters() {
return array(
'accessControl', // perform access control for CRUD operations
array(
'PageTitleFilter + view, create, update, delete, admin',
'controller' => $this
),
);
}
}
And then you put a file protected/messages/es/app.php with the translations of each action, like:
return array(
'view'=>'ver',
'delete'='eliminar'
);
link: http://www.yiiframework.com/doc/guide/1.1/es/topics.i18n#locale-and-language
If you want to change the default pageTitle, you can do in any action:
$this->pageTitle= 'My page title';
If you don't want multi-language, remove the Yii::t('app') function!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With