<?php
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class ExtendedController extends CController
{
public function render($view, $data = null, $return = false)
{
if ($this->beforeRender($view))
{
parent::render($view, $data, $return);
}
}
public function beforeRender($view)
{
return true;
}
}
<?php
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class Controller extends ExtendedController {
/**
* @var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout = '//layouts/column1';
/**
* @var array context menu items. This property will be assigned to {@link CMenu::items}.
*/
public $menu = array();
/**
* @var array the breadcrumbs of the current page. The value of this property will
* be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs = array();
public $pageTitle;
public $pageKeywords;
public $pageDescription;
public function beforeRender($view) {
$this->pageTitle = Yii::app()->params['title'] . ' ' . Yii::app()->params['description'];
if (!empty($this->pageDescription)) {
Yii::app()->clientScript->registerMetaTag($this->pageDescription, 'description');
} elseif(empty($this->pageDescription)) {
$this->pageDescription = Yii::app()->config->get('meta_description');
Yii::app()->clientScript->registerMetaTag($this->pageDescription, 'description');
}
if (!empty($this->pageKeywords)) {
Yii::app()->clientScript->registerMetaTag($this->pageKeywords, 'keywords');
} elseif(empty($this->pageKeywords)) {
Yii::app()->clientScript->registerMetaTag(Yii::app()->config->get('site_keywords'), 'keywords');
}
return true;
}
}
After posting my questions I decided to get some sleep and as I started to doze off It occurred to me that I would probably need to set the variables in the controller.
I have come across class files like this … http://code.google.com/p/yii/source/browse/tags/1.1.9/framework/web/helpers/CHtml.php#189 … when searching different things, but I was under the impression that those pages were describing classes that were already set in the framework ready to use. ‘Ready to use’ is a bit clearer to me now. Ready to use in a controller to set the variables, not ready to use on a view page.
Anyway,
The code you offered, which I have seen at this link …
You don’t need this. beforeRender() is implemented in the latest Yii versions. Just extend all your controllers from Controller (which contains the meta tag logic).
That means the there is no "config" component defined in your config.
However I do see a ‘components’ property plus I noticed a config array being used in your code found in your signature link Extension:Config. Is this what my main.php config file is missing…
A generic meta tag can be set using params which displays the same meta content for every page-view, site-wide.
By setting params for static pages meta tags, and using the Simplified Meta Tags code for dynamic pages meta tags, the result is that the dynamic pages will get two meta tags, one from each source.
To get a single, unique meta tag for each type of page then the extension needs to be implemented.
It is my understaning that the extension is for storing the meta tag content in a database then calling it for the appropriate page…
You want to store meta tags in database for the index page. This way you can change the meta tags like the title from the admin backend for various reasons. Of course you could add more config options like ‘moduleName_title’, then you could change the meta title of each module’s index page in the admin backend. Or you could even add tags for every controller and action.
If the tags are not from database, then simply set them in each controller/action. You can also set defaults by simply defining the values in the base controller (Controller.php).
public $pageTitle = 'Some title';
If you want defaults to be loaded from database:
class Controller extends CController
{
public function init()
{
parent::init();
$this->pageTitle = Yii::app()->config->get('meta_title');
}
}
There’s not much sense in using config extension & params at the same time I guess. Just use one or the other and set “dynamic” meta tags within the controller.
Your extension looks a bit much for my simple site, however you did make me think of something I hadn’t thought of; searching the yii framework for an available extension! :headThump: thump you and jacmoe too for not suggesting it in the first place!
I found one that even I could implement, without help! It’s working well on both types of pages. There are parts of it that I am not using, however I might use the full features later.
SeoRecordBehavior - Active record behavior for defining the model URL
SeoControllerBehavior - Controller behavior for setting page meta data
SeoFilter - Controller filter for correcting incorrect URLs
SeoMetaWidget - Widget for rendering page meta data
THANK YOU CHRIS!
…and thank you Yii, I do appreciate your time, you have given me alot to think about.