个性化阅读
专注于IT技术分析

Phalcon视图如何使用?

点击下载

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

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

将视图与控制器集成

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

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

Server Address 193.168.1.1
Phalcon目录 srcmini
Controller Phalcon
Action Intro
Parameter 911

实作

<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
    public function indexAction()
    {
    }
    public function showAction($postId)
    {
        // Pass the $postId parameter to the view
        $this->view->postId = $postId;
    }
}

层次渲染

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

Name File Description
Action View app / views / posts / show.phtml 这是与操作有关的视图。仅在执行showaction时显示。
Controller Layout app/views/layouts/posts.phtml 这是与控制器相关的视图。仅在控制器” posts”中执行的每个动作都将显示它。布局中实现的所有代码将被该控制器中的所有操作重用。
主要布局 app / views / index.phtml 这是主要动作, 将针对应用程序内执行的每个控制器或动作显示。

实作

<!-- app/views/posts/show.phtml -->
<h3>This is show view!</h3>
<p>I have received the parameter <?php echo $postId; ?></p>

<!-- app/views/layouts/posts.phtml -->
<h2>This is the "posts" controller layout!</h2>
<?php echo $this->getContent(); ?>

<!-- app/views/index.phtml -->
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>This is Phalcon Tutorial!</h1>
<?php echo $this->getContent(); ?>
</body>
</html>

输出

Phalcon视图如何使用?1

简单渲染

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

实作

默认组件更换

<?php
use Phalcon\Mvc\View\Simple as SimpleView;
$di->set(
    'view', function () {
        $view = new SimpleView();
        $view->setViewsDir('../app/views/');
        return $view;
    }, true
);

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

<?php
use Phalcon\Mvc\Controller;
class PostsController extends Controller
{
    public function indexAction()
    {
        // Render 'views-dir/index.phtml'
        echo $this->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
<?php
$params = [
    'posts' =>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 中断操作
beforeRender 在开始渲染过程之前触发 Yes
beforeRenderView 在呈现现有视图之前触发 Yes
afterRenderView 渲染现有视图后触发 No
afterRender 完成渲染过程后触发 No
notFoundView 找不到视图时触发 No

实作

<?php
use Phalcon\Events\Event;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Mvc\View;
$di->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
);
赞(0)
未经允许不得转载:srcmini » Phalcon视图如何使用?

评论 抢沙发

评论前必须登录!