Php 在MVC视图中,什么是布局以及如何创建布局

Php 在MVC视图中,什么是布局以及如何创建布局,php,model-view-controller,Php,Model View Controller,我不明白视图中的布局是什么。我以前问过一个关于PHP模板的问题,但我还是不太明白。我假设您为站点创建了一个总体布局,然后在该布局中包含每个特定视图。。。。我想知道如何着手做这件事。另外,模板是否应该只使用html制作,因为我还研究了这些称为helpers的东西。。。。我只是对MVC的视图部分、实际模板以及它们的制作方式感到困惑。如果你们有例子的话,我会用例子来学习 还有一个更重要的问题,假设我有一个表单,用户只有在登录时才会看到,我是在视图中还是在控制器中控制它 我也会这么做 in the co

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

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

我也会这么做

in the controller

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

模板中的

....
编辑


那么,布局中是否有include语句来查看特定的视图模板?这是怎么回事?

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

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

  • 当渲染实际视图时,将保存此内容
  • 将呈现布局视图(内容周围的所有项目),并且您的内容将包含在该输出中
  • 对于您的登录问题,实际上您必须同时执行这两项操作 在控制器和视图上

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

    <?php if($isLogged): ?>
      <form>....</form>
    <?php endif;?>
    
    
    ....
    
    我之所以不愿意回答这个问题,仅仅是因为围绕着这个问题的宗教热情

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

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

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

    我喜欢这样看待它:视图不应该知道数据来自何处,也不应该知道数据将流向何处。模型应该是视图不可知的。控制器对模型中的数据进行网格划分,并将其提供给视图,然后从视图中获取输入并将其过滤到模型中

    下面是一个快速而肮脏的(未经测试-但应该能让人理解)示例:

    App.php(应用程序控制器)

    welcome.php

    <h1> Welcome to this quick and dirty app!</h1>
    All sorts of good HTML, javascript, etc would be put in here!
    
    <form action"/Theapp/login" method="post">
       User: <input id='user' name='user'>
       Pass: <input id='pass' name='pass' type='password'>
       <input type='submit'>
    </form>
    
    Hi <?= $user ?>!<br>
    Here's your menu<br>
    <? foreach ($menu as $option) { ?>
    <div class='menuOption'><a href='/Theapp/<?=$option?>'><?=$option?></a></div>
    <? } ?>
    
    欢迎使用这个又快又脏的应用程序!
    各种优秀的HTML、javascript等都会放在这里!
    
    loginform.php

    <h1> Welcome to this quick and dirty app!</h1>
    All sorts of good HTML, javascript, etc would be put in here!
    
    <form action"/Theapp/login" method="post">
       User: <input id='user' name='user'>
       Pass: <input id='pass' name='pass' type='password'>
       <input type='submit'>
    </form>
    
    Hi <?= $user ?>!<br>
    Here's your menu<br>
    <? foreach ($menu as $option) { ?>
    <div class='menuOption'><a href='/Theapp/<?=$option?>'><?=$option?></a></div>
    <? } ?>
    
    
    用户:
    通过:
    
    menu.php

    <h1> Welcome to this quick and dirty app!</h1>
    All sorts of good HTML, javascript, etc would be put in here!
    
    <form action"/Theapp/login" method="post">
       User: <input id='user' name='user'>
       Pass: <input id='pass' name='pass' type='password'>
       <input type='submit'>
    </form>
    
    Hi <?= $user ?>!<br>
    Here's your menu<br>
    <? foreach ($menu as $option) { ?>
    <div class='menuOption'><a href='/Theapp/<?=$option?>'><?=$option?></a></div>
    <? } ?>
    
    你好
    这是您的菜单

    希望这能有所帮助。

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

    <?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;
    
    ?>
    
    
    
    对不起,您使用的是什么MVC框架?这将有助于构建更好的答案。请注意,“welcome.php”总是被加载,但loginform.php和menu.php仅根据控制器中的逻辑加载。用户看到welcome.php和loginform.php或menu.phpals的连接,注意这是一个CodeIgniter实现