没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\web\View

继承yii\web\View » yii\base\View » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\base\DynamicContentAwareInterface
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/web/View.php

View represents a view object in the MVC pattern.

View provides a set of methods (e.g. render()) for rendering purpose.

View is configured as an application component in yii\base\Application by default. You can access that instance via Yii::$app->view.

You can modify its configuration by adding an array to your application config under components as it is shown in the following example:

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

For more details and usage information on View, see the guide article on views.

公共属性

隐藏继承的属性

属性类型描述被定义在
$assetBundles yii\web\AssetBundle[] List of the registered asset bundles. yii\web\View
$assetManager yii\web\AssetManager The asset manager. yii\web\View
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$blocks array A list of named output blocks. yii\base\View
$cacheStack array|yii\base\DynamicContentAwareInterface[] A list of currently active dynamic content class instances. yii\base\View
$context yii\base\ViewContextInterface The context under which the renderFile() method is being invoked. yii\base\View
$css array The registered CSS code blocks. yii\web\View
$cssFiles array The registered CSS files. yii\web\View
$defaultExtension string The default view file extension. yii\base\View
$dynamicContents yii\base\DynamicContentAwareInterface[] Class instances supporting dynamic contents. yii\base\View
$dynamicPlaceholders array A list of placeholders for embedding dynamic contents. yii\base\View
$js array The registered JS code blocks yii\web\View
$jsFiles array The registered JS files. yii\web\View
$linkTags array The registered link tags. yii\web\View
$metaTags array The registered meta tags. yii\web\View
$params mixed Custom parameters that are shared among view templates. yii\base\View
$renderers array A list of available renderers indexed by their corresponding supported file extensions. yii\base\View
$theme yii\base\Theme|array|string The theme object or the configuration for creating the theme object. yii\base\View
$title string The page title yii\web\View
$viewFile string|boolean The view file currently being rendered. yii\base\View

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addDynamicPlaceholder() Adds a placeholder for dynamic content. yii\base\View
afterRender() This method is invoked right after renderFile() renders a view file. yii\base\View
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
beforeRender() This method is invoked right before renderFile() renders a view file. yii\base\View
beginBlock() Begins recording a block. yii\base\View
beginBody() Marks the beginning of an HTML body section. yii\web\View
beginCache() Begins fragment caching. yii\base\View
beginContent() Begins the rendering of content that is to be decorated by the specified view. yii\base\View
beginPage() Marks the beginning of a page. yii\base\View
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
clear() Clears up the registered meta tags, link tags, css/js scripts and files. yii\web\View
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
endBlock() Ends recording a block. yii\base\View
endBody() Marks the ending of an HTML body section. yii\web\View
endCache() Ends fragment caching. yii\base\View
endContent() Ends the rendering of content. yii\base\View
endPage() Marks the ending of an HTML page. yii\web\View
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
evaluateDynamicContent() Evaluates the given PHP statements. yii\base\View
getAssetManager() Registers the asset manager being used by this view object. yii\web\View
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getDynamicContents() Returns a list of currently active dynamic content class instances. yii\base\View
getDynamicPlaceholders() Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. yii\base\View
getViewFile() yii\base\View
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
head() Marks the position of an HTML head section. yii\web\View
init() Initializes the view component. yii\base\View
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
popDynamicContent() Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class instances. yii\base\View
pushDynamicContent() Adds a class instance supporting dynamic contents to the end of a list of currently active dynamic content class instances. yii\base\View
registerAssetBundle() Registers the named asset bundle. yii\web\View
registerCsrfMetaTags() Registers CSRF meta tags. yii\web\View
registerCss() Registers a CSS code block. yii\web\View
registerCssFile() Registers a CSS file. yii\web\View
registerJs() Registers a JS code block. yii\web\View
registerJsFile() Registers a JS file. yii\web\View
registerJsVar() Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names. yii\web\View
registerLinkTag() Registers a link tag. yii\web\View
registerMetaTag() Registers a meta tag. yii\web\View
render() Renders a view. yii\base\View
renderAjax() Renders a view in response to an AJAX request. yii\web\View
renderDynamic() Renders dynamic content returned by the given PHP statements. yii\base\View
renderFile() Renders a view file. yii\base\View
renderPhpFile() Renders a view file as a PHP script. yii\base\View
setAssetManager() Sets the asset manager. yii\web\View
setDynamicPlaceholders() Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. yii\base\View
trigger() Triggers an event. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述被定义在
findViewFile() Finds the view file based on the given view name. yii\base\View
getRequestedViewFile() yii\base\View
registerAssetFiles() Registers all files provided by an asset bundle including depending bundles files. yii\web\View
renderBodyBeginHtml() Renders the content to be inserted at the beginning of the body section. yii\web\View
renderBodyEndHtml() Renders the content to be inserted at the end of the body section. yii\web\View
renderHeadHtml() Renders the content to be inserted in the head section. yii\web\View

Events

隐藏继承的事件

事件类型描述被定义在
EVENT_AFTER_RENDER yii\base\ViewEvent An event that is triggered by renderFile() right after it renders a view file. yii\base\View
EVENT_BEFORE_RENDER yii\base\ViewEvent An event that is triggered by renderFile() right before it renders a view file. yii\base\View
EVENT_BEGIN_BODY \yii\web\Event An event that is triggered by beginBody(). yii\web\View
EVENT_BEGIN_PAGE yii\base\Event An event that is triggered by beginPage(). yii\base\View
EVENT_END_BODY \yii\web\Event An event that is triggered by endBody(). yii\web\View
EVENT_END_PAGE yii\base\Event An event that is triggered by endPage(). yii\base\View

常量

隐藏继承的常量

常量描述被定义在
PH_BODY_BEGIN '' This is internally used as the placeholder for receiving the content registered for the beginning of the body section. yii\web\View
PH_BODY_END ' ' This is internally used as the placeholder for receiving the content registered for the end of the body section. yii\web\View
PH_HEAD ' ' This is internally used as the placeholder for receiving the content registered for the head section. yii\web\View
POS_BEGIN 2 The location of registered JavaScript code block or files. This means the location is at the beginning of the body section. yii\web\View
POS_END 3 The location of registered JavaScript code block or files. This means the location is at the end of the body section. yii\web\View
POS_HEAD 1 The location of registered JavaScript code block or files. This means the location is in the head section. yii\web\View
POS_LOAD 5 The location of registered JavaScript code block. This means the JavaScript code block will be enclosed within jQuery(window).load(). yii\web\View
POS_READY 4 The location of registered JavaScript code block. This means the JavaScript code block will be enclosed within jQuery(document).ready(). yii\web\View

属性详情

$assetBundles 公共 属性

List of the registered asset bundles. The keys are the bundle names, and the values are the registered yii\web\AssetBundle objects.

参见 registerAssetBundle().

$assetManager 公共 属性

The asset manager. Defaults to the "assetManager" application component.

public yii\web\AssetManager getAssetManager ( )
public void setAssetManager ( $value )
$css 公共 属性

The registered CSS code blocks.

参见 registerCss().

public array $css = []
$cssFiles 公共 属性

The registered CSS files.

参见 registerCssFile().

public array $cssFiles = []
$js 公共 属性

The registered JS code blocks

参见 registerJs().

public array $js = []
$jsFiles 公共 属性

The registered JS files.

参见 registerJsFile().

public array $jsFiles = []
$linkTags 公共 属性

The registered link tags.

参见 registerLinkTag().

public array $linkTags = []
$metaTags 公共 属性

The registered meta tags.

参见 registerMetaTag().

public array $metaTags = []
$title 公共 属性

The page title

public string $title null

方法详情

beginBody() 公共 方法

Marks the beginning of an HTML body section.

public void beginBody()
clear() 公共 方法

Clears up the registered meta tags, link tags, css/js scripts and files.

public void clear()
endBody() 公共 方法

Marks the ending of an HTML body section.

public void endBody()
endPage() 公共 方法

Marks the ending of an HTML page.

public void endPage($ajaxMode false)
$ajaxMode boolean

Whether the view is rendering in AJAX mode. If true, the JS scripts registered at POS_READY and POS_LOAD positions will be rendered at the end of the view like normal scripts.

getAssetManager() 公共 方法

Registers the asset manager being used by this view object.

public yii\web\AssetManager getAssetManager()
return yii\web\AssetManager

The asset manager. Defaults to the "assetManager" application component.

head() 公共 方法

Marks the position of an HTML head section.

public void head()
registerAssetBundle() 公共 方法

Registers the named asset bundle.

All dependent asset bundles will be registered.

public yii\web\AssetBundle registerAssetBundle($name, $position null)
$name string

The class name of the asset bundle (without the leading backslash)

$position integer|null

If set, this forces a minimum position for javascript files. This will adjust depending assets javascript file position or fail if requirement can not be met. If this is null, asset bundles position settings will not be changed. See registerJsFile() for more details on javascript position.

return yii\web\AssetBundle

The registered asset bundle instance

throws yii\base\InvalidConfigException

if the asset bundle does not exist or a circular dependency is detected

registerAssetFiles() 受保护 方法

Registers all files provided by an asset bundle including depending bundles files.

Removes a bundle from $assetBundles once files are registered.

protected void registerAssetFiles($name)
$name string

Name of the bundle to register

registerCsrfMetaTags() 公共 方法 (自版本 2.0.13 可用)

Registers CSRF meta tags.

They are rendered dynamically to retrieve a new CSRF token for each request.

$view->registerCsrfMetaTags();

The above code will result in <meta name="csrf-param" content="[yii\web\Request::$csrfParam]"> and <meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA=="> added to the page.

Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling window.yii.refreshCsrfToken() from yii.js.

public void registerCsrfMetaTags()
registerCss() 公共 方法

Registers a CSS code block.

public void registerCss($css, $options = [], $key null)
$css string

The content of the CSS code block to be registered

$options array

The HTML attributes for the <style>-tag.

$key string

The key that identifies the CSS code block. If null, it will use $css as the key. If two CSS code blocks are registered with the same key, the latter will overwrite the former.

registerCssFile() 公共 方法

Registers a CSS file.

This method should be used for simple registration of CSS files. If you want to use features of yii\web\AssetManager like appending timestamps to the URL and file publishing options, use yii\web\AssetBundle and registerAssetBundle() instead.

public void registerCssFile($url, $options = [], $key null)
$url string

The CSS file to be registered.

$options array

The HTML attributes for the link tag. Please refer to yii\helpers\Html::cssFile() for the supported options. The following options are specially handled and are not treated as HTML attributes:

  • depends: array, specifies the names of the asset bundles that this CSS file depends on.
$key string

The key that identifies the CSS script file. If null, it will use $url as the key. If two CSS files are registered with the same key, the latter will overwrite the former.

registerJs() 公共 方法

Registers a JS code block.

public void registerJs($js, $position self::POS_READY, $key null)
$js string

The JS code block to be registered

$position integer

The position at which the JS script tag should be inserted in a page. The possible values are:

  • POS_HEAD: in the head section
  • POS_BEGIN: at the beginning of the body section
  • POS_END: at the end of the body section
  • POS_LOAD: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
  • POS_READY: enclosed within jQuery(document).ready(). This is the default value. Note that by using this position, the method will automatically register the jQuery js file.
$key string

The key that identifies the JS code block. If null, it will use $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.

registerJsFile() 公共 方法

Registers a JS file.

This method should be used for simple registration of JS files. If you want to use features of yii\web\AssetManager like appending timestamps to the URL and file publishing options, use yii\web\AssetBundle and registerAssetBundle() instead.

public void registerJsFile($url, $options = [], $key null)
$url string

The JS file to be registered.

$options array

The HTML attributes for the script tag. The following options are specially handled and are not treated as HTML attributes:

  • depends: array, specifies the names of the asset bundles that this JS file depends on.
  • position: specifies where the JS script tag should be inserted in a page. The possible values are:
    • POS_HEAD: in the head section
    • POS_BEGIN: at the beginning of the body section
    • POS_END: at the end of the body section. This is the default value.

Please refer to yii\helpers\Html::jsFile() for other supported options.

$key string

The key that identifies the JS script file. If null, it will use $url as the key. If two JS files are registered with the same key at the same position, the latter will overwrite the former. Note that position option takes precedence, thus files registered with the same key, but different position option will not override each other.

registerJsVar() 公共 方法 (自版本 2.0.14 可用)

Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.

public void registerJsVar($name, $value, $position self::POS_HEAD)
$name string

Name of the variable

$value array|string

Value of the variable

$position integer

The position in a page at which the JavaScript variable should be inserted. The possible values are:

  • POS_HEAD: in the head section. This is the default value.
  • POS_BEGIN: at the beginning of the body section.
  • POS_END: at the end of the body section.
  • POS_LOAD: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
  • POS_READY: enclosed within jQuery(document).ready(). Note that by using this position, the method will automatically register the jQuery js file.
registerLinkTag() 公共 方法

Registers a link tag.

For example, a link tag for a custom favicon can be added like the following:

$view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);

which will result in the following HTML: <link rel="icon" type="image/png" href="/myicon.png">.

Note: To register link tags for CSS stylesheets, use registerCssFile() instead, which has more options for this kind of link tag.

public void registerLinkTag($options, $key null)
$options array

The HTML attributes for the link tag.

$key string

The key that identifies the link tag. If two link tags are registered with the same key, the latter will overwrite the former. If this is null, the new link tag will be appended to the existing ones.

registerMetaTag() 公共 方法

Registers a meta tag.

For example, a description meta tag can be added like the following:

$view->registerMetaTag([
    'name' => 'description',
    'content' => 'This website is about funny raccoons.'
]);

will result in the meta tag <meta name="description" content="This website is about funny raccoons.">.

public void registerMetaTag($options, $key null)
$options array

The HTML attributes for the meta tag.

$key string

The key that identifies the meta tag. If two meta tags are registered with the same key, the latter will overwrite the former. If this is null, the new meta tag will be appended to the existing ones.

renderAjax() 公共 方法

Renders a view in response to an AJAX request.

This method is similar to render() except that it will surround the view being rendered with the calls of beginPage(), head(), beginBody(), endBody() and endPage(). By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the view.

参见 render().

public string renderAjax($view, $params = [], $context null)
$view string

The view name. Please refer to render() on how to specify this parameter.

$params array

The parameters (name-value pairs) that will be extracted and made available in the view file.

$context object

The context that the view should use for rendering the view. If null, existing $context will be used.

return string

The rendering result

renderBodyBeginHtml() 受保护 方法

Renders the content to be inserted at the beginning of the body section.

The content is rendered using the registered JS code blocks and files.

protected string renderBodyBeginHtml()
return string

The rendered content

renderBodyEndHtml() 受保护 方法

Renders the content to be inserted at the end of the body section.

The content is rendered using the registered JS code blocks and files.

protected string renderBodyEndHtml($ajaxMode)
$ajaxMode boolean

Whether the view is rendering in AJAX mode. If true, the JS scripts registered at POS_READY and POS_LOAD positions will be rendered at the end of the view like normal scripts.

return string

The rendered content

renderHeadHtml() 受保护 方法

Renders the content to be inserted in the head section.

The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.

protected string renderHeadHtml()
return string

The rendered content

setAssetManager() 公共 方法

Sets the asset manager.

public void setAssetManager($value)
$value yii\web\AssetManager

The asset manager

事件详情

EVENT_BEGIN_BODY event of type \yii\web\Event

An event that is triggered by beginBody().

EVENT_END_BODY event of type \yii\web\Event

An event that is triggered by endBody().