⊗ppMVUFmVwPD 11 of 35 menu

Passing Data to the View in MVC in PHP

Using the second parameter of the render method, you can pass data from the controller to the view. The data should be passed as an associative array. In this case, in the view, the keys of this array will become variables with the corresponding values.

Let's see it in practice. Let's pass an array with three elements to our view:

<?php namespace Project\Controllers; use Core\Controller; class PageController extends Controller { public function act() { return $this->render('page/act', [ 'var1' => 'eee', 'var2' => 'bbb', 'var3' => 'kkk', ]); } } ?>

As you can see, the keys of our array are 'var1', 'var2', and 'var3'. This means that in the view, the following variables will be available, and we can output their values in the desired places in the HTML code. Let's do this:

<div> this is the view of the act action of the test controller </div> <ul> <li><?php echo $var1; ?></li> <li><?php echo $var2; ?></li> <li><?php echo $var3; ?></li> </ul>

Perform the described manipulations, and then access our action via the address bar. Make sure that the text from the view you created appears in the browser.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline