Help with Including a View

Hi … I want load a list of profiles within another page… so I am trying to do something like this:

<div id="adminContainer">

&lt;?php include Yii::app()-&gt;request-&gt;baseUrl.&quot;/protected/views/profile/admin.php&quot;; ?&gt;

</div>

That gives me this error: include(/pretty/protected/views/profile/admin.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

So I tried this: <?php include $_SERVER['DOCUMENT_ROOT']."/pretty/protected/views/profile/admin.php"; ?>

That worked … but I don't want to use the PHP env variable like that … and I get the error that $sort is undefined… b/c I never hit the Profile controller.

So… my question is how to include results by hitting the controller, using the model, and returning the results with view.

Thank you so much to anyone who is able to help me out.  I really appreciate.  I'm just getting started with Yii but I like it a lot so far.

Yii::app()->request->baseUrl returns the base URL, which is not what you want.

You can use

<?php $this->renderPartial('/profile/admin'); ?>

Thanks so much for replaying! That actually helped me with something else, but when I call $this->renderPartial('/profile/admin'); … none of the variables were ever defined because the actionAdmin() function in the controller was never used.

Right now it's working but I'm using JS to go get the info:

	$.post(&quot;/?r=profile/admin&quot;,


		function(data) {


			$(&quot;#adminContainer&quot;).html(data);


		}


	);

By doing this… I hit the controller and get the HTML I want.  Is there a way to do something like:  Yii::ProfileController->actionAdmin() and get the HTML back?

I don't quite understand your need. If you want to pass in data to the admin view, you can do so with renderPartial() just like you do with render() (that is, pass the data array as the second parameter to the method).

You can also directly include the view file just like you did before.

include(Yii::getPathOfAlias('application.views.profile.admin').'.php');

I'm sorry - I'm probably not being very clear.

Let’s use the blog example.  If I go to http://localhost/?r=post/admin then I get a list of posts.  The simple flow of that is:

  1. initiate web app

  2. go to PostController

  3. go to actionAdmin()

  4. get list of posts from Post model

  5. render the contents in /protected/views/post/admin.php wrapped in /protected/views/layouts/main.php

or

renderPartial and that won't include /protected/views/layouts/main.php

That work flow was accomplished by ?r=post/admin

I'm basically trying to accomplish the same thing, but from within the code.  How do I call the actionAdmin() function of the PostController from with another area of the code?

If in the same controller, you can call another action using $this->run($actionID). If in a different controller, you need to use Yii::app()->runController($route).

However, it seems odd to me to use this approach. When don't you simply redirect to this controller action?

Thank you.

The reason I’m doing this is because my page is actually http://localhost/?r=admintools

Within that, I'm using the jQuery UI tabs to show several different tools on different tabs.  So, on Tab #1 … I will call it "profiles" and make a call to Yii:app()->ProfileController(admin) to show a list of profiles.

Maybe it is odd  ???

<?php echo Yii::app()->runController('profile/admin'); ?>

That's what I needed!  Thanks so much and sorry for the confusion.  I need to read the documentation again :slight_smile: