Phalcon7 視圖的使用

2018-10-21 07:14 更新

視圖代表了應(yīng)用程序中的用戶界面. 視圖通常是在 HTML 文件里嵌入 PHP 代碼,這些代碼僅僅是用來展示數(shù)據(jù)。 視圖的任務(wù)是當(dāng)應(yīng)用程序發(fā)生請求時(shí),提供數(shù)據(jù)給 web 瀏覽器或者其他工具。

Phalcon\Mvc\View 和 Phalcon\Mvc\View\Simple 負(fù)責(zé)管理你的MVC應(yīng)用程序的視圖(View)層。

集成視圖到控制器

當(dāng)某個(gè)控制器已經(jīng)完成了它的周期,Phalcon自動(dòng)將執(zhí)行傳遞到視圖組件。視圖組件將在視圖文件夾中尋找一個(gè)文件夾名與最后一個(gè)控制器名相同,文件命名與最后一個(gè)動(dòng)作相同的文件執(zhí)行。例如,如果請求的URL http://127.0.0.1/blog/posts/show/301, Phalcon將如下所示的方式按解析URL:

Server Address127.0.0.1
Phalcon Directoryblog
Controllerposts
Actionshow
Parameter301

調(diào)度程序?qū)ふ乙粋€(gè)“PostsController”控制器及其“showAction”動(dòng)作。對于這個(gè)示例的一個(gè)簡單的控制器文件:

<?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;
    }
}

setVar允許我們創(chuàng)建視圖變量,這樣可以在視圖模板中使用它們。上面的示例演示了如何傳遞 $postId 參數(shù)到相應(yīng)的視圖模板。

分層渲染

Phalcon\Mvc\View 支持文件的層次結(jié)構(gòu),在Phalcon中是默認(rèn)的視圖渲染組件。這個(gè)層次結(jié)構(gòu)允許通用的布局點(diǎn)(常用的視圖)和以控制器命名的文件夾中定義各自的視圖模板

該組件使用默認(rèn)PHP本身作為模板引擎,因此視圖應(yīng)該以.phtml作為拓展名。如果視圖目錄是 app/views ,視圖組件會(huì)自動(dòng)找到這三個(gè)視圖文件。

名稱文件解釋
Action Viewapp/views/posts/show.phtml這是該動(dòng)作相關(guān)的視圖。它只會(huì)在執(zhí)行 “show” 動(dòng)作時(shí)顯示。
Controller Layoutapp/views/layouts/posts.phtml這是該控制器相關(guān)的視圖。它只會(huì) “posts” 控制器內(nèi)每個(gè)動(dòng)作執(zhí)行時(shí)顯示。這個(gè)控制器的所有動(dòng)作將重用這個(gè)布局的全部代碼。
Main Layoutapp/views/index.phtml這是主布局,它將在應(yīng)用程序的每個(gè)控制器或動(dòng)作執(zhí)行時(shí)顯示。

你不需要實(shí)現(xiàn)上面提到的所有文件。在文件的層次結(jié)構(gòu)中 Phalcon\Mvc\View 將簡單地移動(dòng)到下一個(gè)視圖級(jí)別。如果這三個(gè)視圖文件被實(shí)現(xiàn),他們將被按下面方式處理:

<!-- 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 main layout!</h1>

        <?php echo $this->getContent(); ?>

    </body>
</html>

注意方法 $this->getContent() 被調(diào)用的這行。這種方法指示 Phalcon\Mvc\View 在這里注入前面視圖層次結(jié)構(gòu)執(zhí)行的內(nèi)容。

請求生成的HTML的將為:

<!-- app/views/index.phtml -->
<html>
    <head>
        <title>Example</title>
    </head>
    <body>

        <h1>This is main layout!</h1>

        <!-- app/views/layouts/posts.phtml -->

        <h2>This is the "posts" controller layout!</h2>

        <!-- app/views/posts/show.phtml -->

        <h3>This is show view!</h3>

        <p>I have received the parameter 101</p>

    </body>
</html>

命名空間視圖渲染

默認(rèn)啟用,可以關(guān)閉命名空間視圖渲染:

<?php

use Phalcon\Mvc\View;

$di->set('view', function () {

    $view = new View();

    // Disable namespace view render
    $view->disableNamespaceView();

    return $view;
}, true);

如果設(shè)置路由,對上述的URL做如下解析:

Server Address127.0.0.1
Namespaceblog
Controllerposts
Actionshow
Parameter301

視圖組件會(huì)自動(dòng)找到這四個(gè)視圖文件。

名稱文件解釋
Action Viewapp/views/blog/posts/show.phtml這是該動(dòng)作相關(guān)的視圖。
Controller Layoutapp/views/layouts/blog/posts.phtml這是該控制器相關(guān)的視圖。
Namespace Layoutapp/views/layouts/namespace/blogs.phtml這是該命名空間相關(guān)的視圖。
Main Layoutapp/views/index.phtml這是主布局。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)