【问题标题】:View in MVC, what is a layout and how to create one在 MVC 中查看,什么是布局以及如何创建布局
【发布时间】:2009-12-01 02:33:43
【问题描述】:

我不明白视图中的布局是什么。我之前问过question关于PHP模板的问题,但我还是不太明白。我假设您为站点创建了一个总体布局,然后在该布局中包含每个特定视图......我想知道如何去做。此外,是否应该仅使用 html 来制作模板,因为我还查看了这些称为 helpers 的东西......我只是对 MVC 的 View 部分、实际模板以及它们是如何制作的感到困惑。如果你们有的话,我最好通过例子来学习。

还有一个更重要的问题,假设我有一个用户只有在登录时才能看到的表单,我会在视图中还是在控制器中控制它?

我也可以

in the controller

include 'header';
if(isset($_SESSION['userID'])){
    include 'form';
}
include 'footer';

in the template

<html>
<?php if(isset($_SESSION['user_id'])): ?>
  <form>....</form>
<?php endif;?>
</html>

编辑

那么,布局中是否有包含语句来查看特定的视图模板?怎么样?

【问题讨论】:

  • 抱歉,使用的是什么 MVC 框架?这将有助于建立更好的答案。

标签: php model-view-controller


【解决方案1】:

布局是您在主要内容区域周围拥有的任何东西。通常在普通网站上,它可以是任何侧边栏、页眉、页脚。大多数 MVC 框架提供布局以避免在所有视图中重复这些部分。

你可以想象如果你有两个视图级联

  1. 您的实际视图已呈现,此内容已保存
  2. 呈现布局视图(内容周围的所有项目),并且您的内容包含在该输出中

对于您的登录问题,实际上您必须同时做这两个 在控制器和视图上

$this->view->isLogged = isset($_SESSION['userID']);

在视图中

<?php if($isLogged): ?>
  <form>....</form>
<?php endif;?>

【讨论】:

    【解决方案2】:

    我犹豫回答这个问题只是因为围绕着它的宗教狂热。

    要真正了解一般概念背后的问题,请参阅This Wiki Discussion Page 这是围绕 wiki MVC 文章的讨论页面。

    这是我喜欢遵循的经验法则(顺便说一句,我使用 CodeIgniter,听起来你也是):

    “视图”实际上应该没有逻辑。它应该只是 HTML(在网络世界中)和简单地回显变量的 PHP。在您的示例中,您会将表单分解为自己的视图,控制器将确定是否已加载。

    我喜欢这样看:视图不应该知道数据来自哪里或去往哪里。该模型应该与视图无关。控制器将模型中的数据网格化并将其提供给视图 - 它从视图中获取输入并将其过滤到模型中。

    这是一个快速而肮脏(未经测试 - 但它应该明白这一点)的例子:

    Theapp.php(应用控制器)

    class Theapp extends Controller
    {
       var $_authenticated;
       var $_user;
       var $_menu; // array of menus
    
       function __construct()
       {
          session_start();
          if (isset($_SESSION['authenticated']) && $_SESSION['authenticated'])
          {
               $this->_authenticated = $_SESSION['authenticated'];  // or some such thing
               $this->_user = $_SESSION['user'];
          }
          $this->_menu = array("Logout", "Help", "More");
          parent::__construct();
          $this->loadView("welcome"); // loads primary welcome view - but not necessarily a complete "html" page
       }
    
    
       function index()
       { 
          if (!$this->_authenticated) 
             $this->loadView("loginform");
          else
          {
             $viewData['menu'] = $this->_menu;
             $viewData['user'] = $this->_user;
             $this->loadView("menu", $viewData);
          }
       }
    
    
       function login()
       {
          /* code to authenticate user */
       }
    
       function Logout() { /* code to process Logout menu selection */ }
       function Help() { /* code to process Help menu selection */ }
       function More() { /* code to process More menu selection */ }
    }
    

    welcome.php

    <h1> Welcome to this quick and dirty app!</h1>
    All sorts of good HTML, javascript, etc would be put in here!
    

    loginform.php

    <form action"/Theapp/login" method="post">
       User: <input id='user' name='user'>
       Pass: <input id='pass' name='pass' type='password'>
       <input type='submit'>
    </form>
    

    menu.php

    Hi <?= $user ?>!<br>
    Here's your menu<br>
    <? foreach ($menu as $option) { ?>
    <div class='menuOption'><a href='/Theapp/<?=$option?>'><?=$option?></a></div>
    <? } ?>
    

    希望这会有所帮助。

    【讨论】:

    • 请注意,“welcome.php”总是被加载,但 loginform.php 和 menu.php 仅根据控制器中的逻辑加载。用户看到welcome.php 和loginform.php 或menu.php 之一的串联
    • 另请注意,这是一个 CodeIgniter 实现
    【解决方案3】:

    如果您不使用框架,那么拥有布局和视图的简单方法如下:

    <?php
    function layout($layout_params) {
      extract($layout_params);
    
      # Remember: $layout_content must be echoed for the view to be seen.
      ob_start();
      include "layout_page.php";
      $html = ob_get_contents();
      ob_end_clean();
    
      return $html;
    } 
    
    function view($view_params) {
      extract($view_params);
    
      ob_start();
      include "home_page.php";
      $html = ob_get_contents();
      ob_end_clean();
    
      return $html;
    }
    
    #
    # The variable $parameters is extracted and $params becomes a variable in the view as an array,
    # $logged_in is also now avaiable in the view
    # 
    $parameters = array("params" => array("name" => "joe"), "logged_in" => false);
    
    $view_content = view($parameters);  # => Returns the HTML content for home_page.php
    
    # Now we need the layout content to include the view:
    # The layout file will expect a variable called $layout_content to be the html view.
    # So we need to set $layout_content to be $view_content
    $parameters["layout_content"] = $view_content;
    
    # We no longer need $view_content so we can unset the variable
    unset($view_content);
    
    # When $paramters is extracted it will have $layout_content as a variable:
    $layout_content = layout_view($paramters); # => Returns the HTML content for the layout_page.php
    
    # Now send the results to the browser
    echo $layout_content;
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-21
      • 2012-10-16
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多