View rendering

Swoft officially provides a simple view rendering component. Use php native syntax to provide basic layout, internal import of files and more.

Github -


View rendering as an additional standalone component requires manual installation:

  • With the composer command:
 composer require swoft/view 
  • Configured by composer.json:
     "swoft/view": "~2.0.0" 

Configuration component

When you install the view component, swowt will automatically register it. You can configure the view file storage directory

  • The name of the view component registered in the container is: view
  • Bean configuration (file: app/beans.php )
 'view' => [
    // class 配置是可以省略的, 因为 view 组件里已经配置了它
    // 'class' => \Swoft\View\Renderer::class,
    'viewsPath' => dirname(__DIR__) . '/resource/views/',

Component instances can now be obtained from view() OR \Swoft::getBean('view') .

Configuration item description

  • viewsPath view storage path
  • layout default layout file. It is used by default when calling the render() method.
  • suffix default view suffix (default is php )
  • suffixes list of view suffixes allowed by suffixes. Used to determine if a default suffix needs to be added
  • placeholder content used in the layout file placeholder. Default {_CONTENT_}

How to find a view

  • If you do not add a suffix, the default suffix of the configuration will be automatically appended.
  • When using a relative path, the corresponding view file will be found in the view directory we configured.
  • When an absolute path is used, it will be used directly for rendering. (Support for using the path alias @resource/views/my-view.php )

Use view

  • Render a view file by method: view()
  • Render a view file with \Swoft::getBean('view')->rander('view file')
  • You can also use @View() quickly in the action comment of the controller ( 2.0 is not supported yet )

Use example

 * 控制器demo
 * @Controller(prefix="/demo")
class DemoController
     * 视图渲染demo - 没有使用布局文件(请访问 /demo/view)
     * @RequestMapping()
    public function view()
        $data = [
            'name' => 'Swoft',
            'repo' => '',
            'doc' => '',
            'method' => __METHOD__,

        // 将会渲染 `resource/views/site/index.php` 文件
        return view('site/index', $data);

     * 视图渲染demo - 使用布局文件(请访问 /demo/layout)
     * @RequestMapping()
    public function layout()
        $layout = 'layouts/default.php';
        $data = [
            'name' => 'Swoft',
            'repo' => '',
            'doc' => '',
            'method' => __METHOD__,
            'layoutFile' => $layout

        return view('site/content', $data, $layout);

Use layout file

There are two ways to use layout files:

  1. Configure the default layout file in the configuration, then the default will be used even if the layout not set (see the previous section for the available configuration of the view)
  2. As an example, you can manually set up a layout file. It has a higher priority (even if there is a default layout file, the current incoming substitution will be used.)
  3. You can disable layout rendering files by passing layout=false

Load static files

Swoft can provide support for static resource access (provided by swoole), and it is generally recommended to place static files in a public directory under the root directory.

Configuring Http Server

First we need to configure static file processing, add the following configuration in the http server of app/bean.php

     'httpServer' => [
         * @see  HttpServer::$setting
         * @link
        'setting'  => [
            // enable static handle
            'enable_static_handler'    => true,
            // swoole v4.4.0以下版本, 此处必须为绝对路径
            'document_root'            => dirname(__DIR__) . '/public',

The following is an example of a reference ( no need to include public when referring to ):

 // 真实文件为: public/static/some.js
<script type="text/javascript" src="/static/some.js"></script> 

Introduce other view files

To include other view files in the view file, you can use:

  • include(string $view, array $data, bool $outputIt = true)
  • fetch(string $view, array $data)

The difference between the two methods is that fetch() requires you to manually call echo <?= $this->fetch('layouts/default/header') ?>


Variable data has a scope limit. That is, the variable passed to the view cannot be used directly in the included view. It needs to be passed to the child view through the second parameter $data

    <?php $this->include('layouts/default/header', ['logo' => 'xx/yy/logo.jpg']) ?>

    <div class="container">
        <!-- Content here -->
        <div id="page-content" style="padding: 15px 0;">{_CONTENT_}</div>
        <?php $this->include('layouts/default/footer') ?>