⊗ppMVUFmVw 10 of 35 menu

Views in MVC in PHP

Let's now add views to our actions. According to the rules of our framework, views are stored in the folder /project/views/.

Furthermore, our framework follows a convention: the name of the view file must match the name of the action and be placed in a folder with the same name as the controller. For example, if we have a controller PageController, then all its views should be stored in the folder /project/views/page/. The view file itself must have the extension .php. For instance, if the controller PageController has an action act, then its view should be located at /project/views/page/act.php.

In order to assign a view to an action, that action must call the method render, passing it the name of the view as a parameter. The view name must consist of the controller name in lowercase, followed by the action name after a /.

The method render does not need to be implemented in the controller - it is inherited from the parent class Core\Controller. Moreover, for correct operation, the result of the method should be returned via return.

So, let's say we have a controller Page with an action act. Let's bind a view to this action:

<?php namespace Project\Controllers; use Core\Controller; class PageController extends Controller { public function act() { return $this->render('page/act'); } } ?>

Now let's create the file with the HTML code of our view:

<div> this is the view for the act action of the page controller </div>

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