Basically speaking, you could move all of that view code into your controller action, then send the resulting data to your view file for display, in the same way you are sending the $servicesData to your view currently.
Now if I’m following what you want to do, you could then re-write your controller code to look something like this:
class DefaultController extends Controller
{
public function actionRequirements()
{
$servicesData = ServicesUtil::checkServices();
$messages = array();
if (count($servicesData[2]) > 0) {
if (count($servicesData[2][1]) > 0)
{
foreach($servicesData[2][1] as $failedRequiredService)
{
$messages['failed'][] = $failedRequiredService['message'];
}
}
if (count($servicesData[2][2]) > 0)
{
foreach($servicesData[2][2] as $failedOptionalService)
{
$messages['failed'][] = $failedOptionalService['message'];
}
}
}
if (count($servicesData[1]) > 0)
{
foreach($servicesData[1] as $passedService)
{
$messages['passed'][] = $passedService['message'];
}
}
$this->render('requirements', array('messages'=>$messages);
}
}
I will add that you may want to implement a $messages display app wide(i.e. your /views/layouts/main.php file), and I would also take a look at this wiki to see if Yii’s flash messaging system would suit your purposes better: http://www.yiiframew…flash-messages/
You’re welcome Alex, I’ll try to elaborate for you.
By moving the view code I shared into your /views/layouts/main.php view file, you could then set the $messages array in other controllers and actions and the $messages array could be displayed in any view of your app.
You would likely then want to standardize the display of the $messages based on the $type of message being displayed too. For example, have green success messages, and red error messages. This is just an idea I use though. Yii’s flash messaging could suit app wide user messaging better, depending on how you want to implement such a feature. I tend to use a combination of both, as yii’s flash messaging relies on using $this->redirect(‘somewhere’); in your controller, and the Ajax requests I typically use don’t include a redirect() in the controller action.
In reading your code through a few more times, I’m thinking app wide messaging wasn’t really your original goal, though it is certainly a useful feature to add to some apps. I[size=“2”]n hindsight i[/size][size=“2”]t sounds more like the data from your requirements action should be restricted to that single view. If you just change $messages to something like $requirements, to avoid confusion, I think my code would suit passing an array of data to your view, displaying each requirement, and whether or not the requirement passed or failed.[/size]