Yii Framework Forum: How To Treat A Gii Generated View Like A "views/site/something.php" View? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Treat A Gii Generated View Like A "views/site/something.php" View? Rate Topic: -----

#1 User is offline   Diego Michel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 30-May 13

Posted 31 May 2013 - 10:24 AM

I have been using Yii for a couple weeks now and have addressed several problems/doubts with this forum, it's a great resource! Now, I have a couple problems that I have been struggling with for a while and have no other idea but to ask.

I followed Larry Ullman's tutorial "Learning the Yii Framework"

I created a yii app, I bound it with a database with previously created employee and department tables and finally generated the models, controllers and views using Gii.

My problems are:

1. When I try to render let's say "views/employee/index.php" inside "views/site/index.php" I cannot find the way to make it work.

If I try:

SiteController.php

public function actionIndex()
{
...
$this->render('employee',array('model'=>$model));
...


I get:

CException
SiteController cannot find the requested view "employee".


While for "views/site/contact" and "views/site/login" it works.

What is the way to work with Gii generated views?


2. When I integrate again for example "views/employee/index.php" into the main menu, it doesn't show when that page is active. (for some reason it doesn't add the active class to the li element (<li class="active">).

I added the following code to "views/layouts/main.php":
array('label'=>'employee', 'url'=>array('/employee')),


Any help would be very appreciated.

Thanks in advance

Diego
0

#2 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 31 May 2013 - 03:09 PM

The controller's class/filename and the view directory should match if you want to call something like this.
$this->render('employee',array('model'=>$model));

If the above code is run from the site controller, then by default, Yii will look for views/site/employee.php


So, try this
$this->render('/employee/index',array('model'=>$model)); // tells Yii to look under views/employee/...


As for the menu's active class, try this
array('label'=>'employee', 'url'=>array('/employee'), 'active' => Yii::app()->controller->id == 'employee'),


Matt
0

#3 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 31 May 2013 - 03:41 PM

If you want to render the an employee view within site/index look at the renderPartial method. Use it in the site/index.php view.
// site/index.php
...
$this->renderPartial('/employee/index', array(
    'dataProvider' => ....    
));

Matt



0

#4 User is offline   Diego Michel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 30-May 13

Posted 31 May 2013 - 04:42 PM

Awesome! Thanks a lot for the quick response Matt, my problem number 2 got solved.

Regarding the problem number 1, I tried both ways that you mentioned (render and renderPartial) and in both it complains about the following variable:

Undefined variable: dataProvider

C:\xampp\htdocs\test\protected\views\employee\index.php(18)

06     'Employees',
07 );
08 
09 $this->menu=array(
10     array('label'=>'Create Employee', 'url'=>array('create')),
11     array('label'=>'Manage Employee', 'url'=>array('admin')),
12 );
13 ?>
14 
15 <h1>Employees</h1>
16 
17 <?php $this->widget('zii.widgets.CListView', array(
18     'dataProvider'=>$dataProvider,
19     'itemView'=>'_view',
20 )); ?>


I assume this is happening because EmployeeController's method "actionIndex()" (which provides that variable) is not being executed.

I really can't figure out the appropriate way.

Thanks! :)

Diego
0

#5 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 31 May 2013 - 11:42 PM

You're exactly right but you don't need to execute the method. All you need to do is create the dataProvider in siteController and pass it to your view. You can literally copy/paste the code.
//siteController
public function actionIndex()
{
    .....
    $employeeDataProvider = new CActiveDataProvider('Employee')
    ....

    $this->render('index', array(
        'employeeDataProvider' => $employeeDataProvider
    ));
}

Next, if you're trying to show a list of employees on the site's homepage, you want something like this
// view/site/index
// Copy the relevant code from employee/index

<h1>Employees</h1>
 
 <?php $this->widget('zii.widgets.CListView', array(
 	'dataProvider'=>$employeeDataProvider,
 	'itemView'=>'/employee/_view',
 )); ?>


Matt
0

#6 User is offline   Diego Michel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 30-May 13

Posted 03 June 2013 - 07:55 AM

Thanks Matt!

I think I'll go for the first option, I will copy the code from EmployeeController's method "actionIndex()" into SiteController's "actionIndex()".

The second one wouldn't work for me since I did put the tutorial just as an example, but the app I started working in has been in development for about one year and is a bit more complex.

In both cases you clarified my doubts and helped me substancially to understand more Yii.

Best regards!

Diego
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users