📜  Phalcon视图

📅  最后修改于: 2021-01-07 09:21:49             🧑  作者: Mango

阳台景观

视图表示应用程序的前端。它由嵌入PHP代码中的HTML文件组成,这些PHP文件创建了应用程序的视图。 View将数据从您的应用程序提供给Web浏览器。

Phalcon \ Mvc \ ViewPhalcon \ Mvc \ View \ Simple负责管理MVC应用程序的视图层。

将视图与控制器集成

当控制器完成其功能时,视图将自动集成。整个视图组件将在具有最后一个控制器的相同文件名的视图文件夹中查找。

范例:如果要求网址193.168.1.1/javatpoint/phalcon/intro/911

Server Address 193.168.1.1
Phalcon Directory Javatpoint
Controller Phalcon
Action Intro
Parameter 911

实作

view->postId = $postId;
    }
}

层次渲染

它是视图渲染的默认组件,位于目录Phalcon \ MVC \ View下。它的组件自动使用PHP本身作为模板引擎。它具有扩展名.phtml并且视图组件将找到以下3个文件。

Name File Description
Action View app/views/posts/show.phtml This is the view related to the action. It only will be shown when the showaction is executed.
Controller Layout app/views/layouts/posts.phtml This is the view related to the controller. It only will be shown for every action executed within the controller “posts”. All the code implemented in the layout will be reused for all the actions in this controller.
Main Layout app/views/index.phtml This is main action it will be shown for every controller or action executed within the application.

实作


This is show view!

I have received the parameter

This is the "posts" controller layout!

getContent(); ?> Example

This is Phalcon Tutorial!

getContent(); ?>

输出:

简单渲染

它是Phalcon \ MVC \ View的替代组件,位于Phalcon \ MVC \ View \ Simple下。它类似于MVC \ View,但缺乏层次结构。它允许开发人员控制更改后的视图及其位置。

实作

默认组件更换

set(
    'view',
    function () {
        $view = new SimpleView();
        $view->setViewsDir('../app/views/');
        return $view;
    },
    true
);

现在,要渲染,我们调用render()方法

view->render('index');
        // Render 'views-dir/posts/show.phtml'
        echo $this->view->render('posts/show');
        // Render 'views-dir/index.phtml' passing variables
        echo $this->view->render(
            'index',
            [
                'posts' =>Posts::find(),
            ]
        );
        // Render 'views-dir/posts/show.phtml' passing variables
        echo $this->view->render(
            'posts/show',
            [
                'posts' =>Posts::find(),
            ]
        );
    }
}
Declaring simple() method
Posts::find(),
];
// Phalcon\Mvc\View
$view = new \Phalcon\Mvc\View();
echo $view->render('posts', 'show', $params);
// Phalcon\Mvc\View\Simple
$simpleView = new \Phalcon\Mvc\View\Simple();
echo $simpleView->render('posts/show', $params);
?>

查看活动

Phalcon \ Mvc \ View和Phalcon \ Mvc \ View \ Simple可以将事件发送到EventsManager(如果存在)。

Event Name Triggered Break Operation
beforeRender Triggered before starting the render process Yes
beforeRenderView Triggered before rendering an existing view Yes
afterRenderView Triggered after rendering an existing view No
afterRender Triggered after completing the render process No
notFoundView Triggered when a view was not found No

实作

set(
    'view',
    function () {
        // Create an events manager
        $eventsManager = new EventsManager();
        // Attach a listener for type 'view'
        $eventsManager->attach(
            'view',
            function (Event $event, $view) {
                echo $event->getType(), ' - ', $view->getActiveRenderPath(), PHP_EOL;
            }
        );
        $view = new View();

        $view->setViewsDir('../app/views/');
        // Bind the eventsManager to the view component
        $view->setEventsManager($eventsManager);
        return $view;
    },
    true
);