Home  >  Article  >  PHP Framework  >  Where to register yii2 view

Where to register yii2 view

(*-*)浩
(*-*)浩Original
2019-12-14 09:47:202449browse

Where to register yii2 view

View represents the view object in the MVC pattern.

View provides a set of methods (such as render()) for rendering purposes. (Recommended learning: yii framework )

default, configure the view to the application component in YII \ Base \ Application. You can access this instance via Yii::$app->view.

You can modify the configuration of the application by adding an array of components as shown in the following example:

'view' => [
    'theme' => 'app\themes\MyTheme',
    'renderers' => [
        // you may add Smarty or Twig renderer here
    ]
    // ...

Views are part of the MVC architecture. They are the code responsible for presenting data to the end user. In web applications, views are usually created using view templates, which are PHP script files that mainly contain HTML code and PHP code for presentation.

They are managed by the View Application component, which provides common methods to simplify the composition and rendering of views. For simplicity, we usually refer to view templates or view template files as views.

Creating Views

As mentioned before, views are just PHP scripts mixed with HTML and PHP code. Below is the view showing the login form. As you can see, the PHP code is used to generate dynamic content such as page titles and forms, while the HTML code organizes them into displayable HTML pages.

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\widgets\ActiveForm */
/* @var $model app\models\LoginForm */

$this->title = &#39;Login&#39;;
?>
<h1><?= Html::encode($this->title) ?></h1>

<p>Please fill out the following fields to login:</p>

<?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, &#39;username&#39;) ?>
    <?= $form->field($model, &#39;password&#39;)->passwordInput() ?>
    <?= Html::submitButton(&#39;Login&#39;) ?>
<?php ActiveForm::end(); ?>

In a view, you have access to $this, which references the view component to manage and render this view template.

In addition to $this, there may be other predefined variables in the view, such as $model in the above example. These variables represent data pushed into the view by the controller or other object that triggered the view's rendering.

Tip: Predefined variables are listed in a comment box at the beginning of the view so that the IDE can recognize them. This is also a great way to record your points.

The above is the detailed content of Where to register yii2 view. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn