Using Array Data In A View

Hello,

Being quite new to yii and the MVC approach, I’m still in the “what goes where?” phase, and so I’m sorry in advance if what I ask seems obvious.

I’m trying to build a basic requirement page (php version, $_SERVER etc), so I wrote an utilitary class used in one of my controller action:




class DefaultController extends Controller

{

    public function actionRequirements()

    {

        $servicesData = ServicesUtil::checkServices();

        $this->render('requirements', array('servicesData' => $servicesData));

    }

}



$servicesData looks like:




Array

(

    [1] => Array <-- Requirement is met

        (

            [0] => Array

                (

                    [service] => PHP

                    [message] => PHP version ok (5.4.3).

                )

            [1] => Array

                (

                    [service] => ServerVariable

                    [message] => $_SERVER is accessible.

                )

        )

    [2] => Array <-- Requirement is not met

        (

            [1] => Array <-- Service required

                (

                    [0] => Array

                        (

                             [service] => Curl

                             [message} => Curl extension is not loaded.

                        )

                )

            [2] => Array <-- Service optional

                (

                )

        )

)



And my view "requirements" looks like:




if (count($servicesData[2]) > 0)

{

    if (count($servicesData[2][1]) > 0)

    {

        foreach($servicesData[2][1] as $failedRequiredService)

        {

            echo $failedRequiredService['message'];

        }

        echo '<br/><br/>';

    }

    if (count($servicesData[2][2]) > 0)

    {

        foreach($servicesData[2][2] as $failedOptionalService)

        {

            echo $failedOptionalService['message'];

        }

        echo '<br/><br/>';

    }

}

if (count($servicesData[1]) > 0)

{

    foreach($servicesData[1] as $passedService)

    {

        echo $passedService['message'];

    }

}



From what I understand, there should be far less logic in my view. So is there someone who could explain to me how to properly write this view ?

Many thanks for considering my request

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);

}

}



…Resulting in a much more simplified view file:




foreach($messages as $type => $msgs)

{

	foreach($msgs as $msg)

	{

		echo $type . ": " . $msg . "<br>";

	}

}



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/

Thanks a lot for your help, it’s much appreciated. I think I’ll go for the flash messages as you suggested.

Would you care to elaborate what you mean by implementing a $messages display app wide?

Thanks again.

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]