title: Adding variable for the context on a view, on Laravel 4
body: <p>Lets say that I have a View that uses 3 variables, while developing on Laravel 4:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;Name: {{name}}&lt;/li&gt;
      &lt;li&gt;Surname: {{surname}}&lt;/li&gt;
      &lt;li&gt;Age: {{age}}&lt;/li&gt;
  &lt;ul&gt;
  </code></pre>
  
  <p>In my controller, I could be adding the data to the view the following way.</p>
  
  <pre><code>$context = array('name' =&gt; $name, 'surname' =&gt; $surname, 'age' =&gt; $age);
  
  return View::make('myview');
  </code></pre>
  
  <p>But this code isnt completely DRY, since I am writing twice the name of the variable. It would be optimal if I could do:</p>
  
  <pre><code>$context = array($name, $surname, $age);
  
  return View::make('myview', $context);
  </code></pre>
  
  <p>This code seems tighter. But unfortunately, the view will give me an error "Undefined variable: name".</p>
  
  <p>Is there any way to achieve this with Laravel?</p>
  
  <p><strong>UPDATE:</strong> Another option, as stated in some answers is using compact.</p>
  
  <pre><code>$context = compact('name', 'surname', 'age')
  
  return View::make('myview', $context);
  </code></pre>
  
  <p>But the problem here is that you have to create all your context variable in one go. And programming is not always so convenient sadly. Sometimes you will want to be adding your elements to the context, as you go progressing through the function.</p>
  
  <p>For instance:</p>
  
  <pre><code>$context = array($name, $surname, $age);
  
  if ($age &gt; 18){
      $adult = "He is adult";
      array_push($context, $adult);
  }
  
  return View::make('myview', $context);
  </code></pre>
  
  <p>So as you see, if I could use arrays, it wouldnt be a problem because I could push new elements into the $context with <code>array_push</code>. But I can not use arrays that way, unfortunately. And compact wouldn't allow me to push elements inside the context, would it?</p>