@aniya.jaskolski
In Yii, creating a view involves the following steps:
Here is an example of a simple view in Yii:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!-- views/site/index.php --> <h1>Welcome to my website!</h1> <p>This is the homepage of my website.</p> <?php use yiihelpersHtml; echo Html::beginForm(['site/contact'], 'post'); echo Html::input('text', 'name', '', ['placeholder' => 'Enter your name']); echo Html::submitButton('Submit', ['class' => 'btn btn-primary']); echo Html::endForm(); ?> |
In this example, the view displays a header, a paragraph of text, and a simple contact form. The form is generated using Yii's HTML helper class. When the form is submitted, it will send a POST request to the site/contact
action of the controller.
To render this view from a controller action, you can use the $this->render()
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// controllers/SiteController.php namespace appcontrollers; use yiiwebController; class SiteController extends Controller { public function actionIndex() { return $this->render('index'); } } |
In this example, the SiteController
class defines an action named actionIndex()
that simply renders the index.php
view using the $this->render()
method. The view file is located in the views/site
directory.