0

I have the following model:

class Person
{
    public $name;
    function __Construct( $name )
    {
        $this->name = $name;
    }
}

I have the following controller:

class NavigationController extends Controller
{
    public function indexAction()
    {
        $people = array(
            new Person("James"),
            new Person("Bob")
        );
        return $this->render('FrameworkBundle:Navigation:index.html.php', $people);
    }
}

How do I get access to the model array in the view. Is there a way to access the model directly or do I have to assign a property like so:?

class NavigationController extends Controller
{
    public function indexAction()
    {
        $people = array(
            new Person("James"),
            new Person("Bob")
        );
        return $this->render('FrameworkBundle:Navigation:index.html.php', array( "model" => $people ) );
    }
}

View:

<?php 
    foreach( $model as $person )
    {
       echo $person->title;
    }
?>

The problem with the above will be that it can be changed by a user to

return $this->render( 'FrameworkBundle:Navigation:index.html.php', array( "marybloomingpoppin" => $people ) );

1 Answer 1

0

With the example view you used, you already had the correct implementation:

class NavigationController extends Controller
{
    public function indexAction()
    {
        $people = array(
            new Person("James"),
            new Person("Bob")
        );
        return $this->render('FrameworkBundle:Navigation:index.html.php', array( "model" => $people ) );
    }
}

You mentioned the concern that somebody could change the assignment in the controller, but this is something you always have if somebody changes the name of a variable only in one place and not in all. So I don't think this is an issue.

4
  • Unfortunately my experience of changing variables extends far past this. I had called some array( "people" => $people ).. And another colleague did this with array( "users" => $peopleRepository ). When it came to the third colleague, he was utterly confused for the 2 weeks I was on holiday because this variable was different in the view (was there a separate model he was missing?) (this had happened across the entire application)... In C#.NET MVC (although you have a viewbag) everything is access by @model in the view and is to the best of its ability, not a confusion!
    – Jimmyt1988
    Oct 9, 2013 at 16:33
  • This is then not a technical question but a matter of taste and naming convention. I usually give lists of entities the plural name of my entity, so if I have an array of Users I call it array('users' => $whateverVariable) so that at least in the view the acces to a List of users is always the same.
    – m0c
    Oct 10, 2013 at 8:13
  • @moc it's a programming question... I don't know how to do it another way, what's the code. I don't have a care about naming conventions, I didn't ask if it was a better way, I asked how to do it. this is completely a programming question.
    – Jimmyt1988
    Oct 11, 2013 at 14:49
  • Then I unfortunately don't understand the question. Because the code that should work was already provided by you. And that is what I tried to pointed out. Besides that I don't understand the question :(
    – m0c
    Oct 13, 2013 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.