Hey, everyone!
I’m having trouble pinpointing why my menu and breadcrumbs are not being rendered by one of my controllers. I’ve created this one “by hand,” that is, not using Gii. The ones that were created through Gii do show the menu and breadcrumbs, but not this controller. I’d like to focus just on the basic index view, since that’s pretty straightforward and simple. Here is the controller in question:
<?php
class TextSnippetController extends Controller
{
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionIndex() {
// Find all TextSnippets.
$xmlDoc = Yii::getPathOfAlias('xml') . '/as/snippets.xml';
$snippet = new TextSnippet;
$rawData = $snippet->findAll($xmlDoc);
$dataProvider=new CArrayDataProvider($rawData, array('keyField'=>'identity'));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
public function actionView($id)
{
// Get the correct snippet of text
$model = $this->loadModel($id);
$this->render('view', array(
'model'=>$model,
));
}
/**
* Method for handling a request to create
* a new TextSnippet.
*/
public function actionCreate() {
$model = new TextSnippet;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['TextSnippet'])) {
$model->attributes = $_POST['TextSnippet'];
$valid = $model->validate();
if($valid) {
$this->_save($model);
$this->redirect($this->redirect(array('view', 'id'=>$model->identifier)));
}
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Calls TextSnippet::save() and does any necessary
* preprocessing and/or postprocessing.
*
* @param TextSnippet $model
*/
private function _save($model) {
$model->save();
}
public function loadModel($id) {
// Set the value of $doc to conform to the XML document we want to access.
$xmlDoc = Yii::getPathOfAlias('xml') . '/as/snippets.xml';
return TextSnippet::getInstance($id, $xmlDoc);
}
// Uncomment the following methods and override them if needed
/*
public function filters()
{
// return the filter configuration for this controller, e.g.:
return array(
'inlineFilterName',
array(
'class'=>'path.to.FilterClass',
'propertyName'=>'propertyValue',
),
);
}
public function actions()
{
// return external action classes, e.g.:
return array(
'action1'=>'path.to.ActionClass',
'action2'=>array(
'class'=>'path.to.AnotherActionClass',
'propertyName'=>'propertyValue',
),
);
}
*/
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='entity-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
I have a pretty simple index.php file for a view:
<?php
$this->breadcrumbs=array(
'Snippets',
);
$this->menu=array(
array('label'=>'Add New Snippet', 'url'=>array('create')),
array('label'=>'Manage Snippets', 'url'=>array('admin')),
);
?>
<h1>Snippets</h1>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
As you can see, both menu and breadcrumbs are defined in index.php. My controller extends Controller (rather than CController) so that I have menu and breadcrumbs as member variables. But when render() is called by the controller, these are not included in the output. I’ve stepped through the code, but I haven’t yet caught exactly where the app should expect to find the menu and breadcrumbs arrays and parse them. I can see that they do, in fact, exist, but I don’t see anything trying to use them. Ideas?
Thanks!